C# 向 DataGridView 插入和更新内容不起作用!

发布于 2024-10-11 09:10:04 字数 1990 浏览 7 评论 0原文

我在这里遇到了一个非常麻烦的问题,查询似乎显然是正确的,但它没有返回任何数据行...

基本上问题是在插入之后,我进行选择以获取变量的自动增量编号(NAlbum)(我正在使用 C#),这是一段困扰我的代码

            //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;

提前致谢, 路易斯·达·科斯塔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来日方长 2024-10-18 09:10:04

第一。您需要参数化您的查询以获得更清晰的语法。

示例:

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()                      
}

将结果加载到数据 gridview 后,然后循环所有值
然后使用您的插入语句。

例子:

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();

 }

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();

 }
妄司 2024-10-18 09:10:04

大家好,

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 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)

梦途 2024-10-18 09:10:04
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文