开发者

C# Insert and Update content to DataGridView do not work !

开发者 https://www.devze.com 2023-02-04 05:25 出处:网络
I got a really troublesome issue here, the query seems to be apparently corrent but it returns no rows of data...

I got a really troublesome issue here, the query seems to be apparently corrent but it returns no rows of data...

Basically the issue is after an insert, I make a select to obtain the auto increment number (NAlbum) to a variable (I'm using C#), here's the piece of code that's troubling me

            //Associar ID de Artista
            ClassBD.DBMyReader(
                "SELECT NArtista " +
                "FROM Artistas " +
                "WHERE (Nome = '" + CBBoxAddArtista.Text + "')");
            ClassBD.myReader.Read();
            temptabelas[0] = ClassBD.myReader.GetInt32(0);

            //Associar ID da Editora
            ClassBD.DBMyReader(
                "SELECT NEditora " +
                "FROM Editora " +
                "WHERE (Nome = '" + CBBoxAddEditora.Text + "')");
            ClassBD.myReader.Read();
            temptabelas[1] = ClassBD.myReader.GetInt32(0);

            //Associar ID da Media
            ClassBD.DBMyReader(
                "SELECT NMedia " +
                "FROM Media " +
                "WHERE (Nome = '" + CBBoxAddMedia.Text + "')");
            ClassBD.myReader.Read();
            temptabelas[2] = ClassBD.myReader.GetInt32(0);

            //Associar ID do Genero
            ClassBD.DBMyReader(
                "SELECT NGenero " +
                "FROM Genero_de_Musica " +
                "WHERE (Nome = '" +开发者_高级运维 CBBoxAddGenero.Text + "')");
            ClassBD.myReader.Read();
            temptabelas[3] = ClassBD.myReader.GetInt32(0);

            ClassBD.DBMyInsertCommand("INSERT INTO Albuns " +
                "(NArtista, NEditora, NGeneroDeMusica, NMedia, Nome, [Ano de Edição])" +
                "VALUES (" + temptabelas[0] + "," + temptabelas[1] + "," + temptabelas[2] + "," + temptabelas[3] + ",'" + TxtAddMusicaAlbum.Text + "'," + int.Parse(TxtAddAnoEdicao.Text) + ")");

            ClassBD.DBMyReader("SELECT MAX(NAlbum) AS Actual " +
                               "FROM Albuns");
            tempnalbum = ClassBD.myReader.GetInt32(0);

            musicasBindingSource.Filter = "NAlbum = " + tempnalbum;

Thanks in advance, Luis Da Costa


1st. You Need to parametized your query for cleaner syntax.

Example:

string CommandText = "select NArista from Artista where Nome=@Nome";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Nome",CBBoxAddArtista.Text);  
rdr = cmd.ExecuteReader();  
while(rdr.Read())
{
 temptabelas[0] = rdr["NAtista"].ToString()                      
}

After Getting the Result loaded to Data gridview then loop all the values Then use your insert statement.

Example:

foreach (DataGridViewRow dr in Datagridview1.Rows)
{
//Do your Code
"INSERT INTO Albuns " +
 "(NArtista, NEditora, NGeneroDeMusica, NMedia, Nome, [Ano de Edição])" +
"VALUES (@1,@2,@3,@4,@5,@6)";
// Add parameter
cmd.Parameters.AddWithValue("@1",(string) dr.Cells["Natista"].Value.ToString()); 
.
.
.
.
cmd.Parameters.AddWithValue("@6",int.Parse(TxtAddAnoEdicao.Text)


cmd.ExecuteNonQuery();

 }


HI Everyone,

ClassBD.DBMyInsertCommand("INSERT INTO Albuns " +
                "(NArtista, NEditora, NGeneroDeMusica, NMedia, Nome, [Ano de Edição]) " +
                "VALUES (" + temptabelas[0] + "," + temptabelas[1] + "," + temptabelas[2] + "," + temptabelas[3] + ",'" + TxtAddMusicaAlbum.Text + "'," + int.Parse(TxtAddAnoEdicao.Text) + ")");

            OleDbConnection connection = new OleDbConnection(ClassBD.MyConnectionString);
            connection.Open();
            OleDbCommand MyCommand = new OleDbCommand("SELECT NAlbum FROM Albuns WHERE (Nome = @Nome)", connection);
            MyCommand.Parameters.AddWithValue("@Nome", TxtAddMusicaAlbum.Text);
            OleDbDataReader myReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
            ClassBD.tempnalbum = myReader.GetInt32 ;

"Hi, I've asked this very question yesterday, you told me to parameterize the query, and so I did... However, it still comes up with no rows... Help?" "(The Insert is correct, I've checked it's entry)


Private Sub BoundLoadButton_Click(ByVal sender As System.Object, 
                                  ByVal e As System.EventArgs) 
                                  Handles BoundLoadButton.Click
     swatch.Reset()
     swatch.Start()
     Cursor = Cursors.WaitCursor
     Try
        Using BoundObject As New UnboundClass(mMySQLConnectionString)
           Call BoundObject.BoundDataLoading(UnboundDataGridView, _
                                             RecordCountTextBox, _
                                             mErrorMsgString)
           If Not IsNothing(mErrorMsgString) Then
              Cursor = Cursors.Default
              MessageBox.Show(mErrorMsgString, _
                              Me.Text, _
                              MessageBoxButtons.OK, _
                              MessageBoxIcon.Error)
           End If
        End Using
     Catch exError As Exception
        MessageBox.Show(exError.Message, _
                        Me.Text, _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
     End Try
     Cursor = Cursors.Default
     swatch.Stop()
     mTimeDouble = swatch.ElapsedMilliseconds * 0.001
     BoundTimeTextBox.Text = mTimeDouble.ToString
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消