C#插入和更新内容到DataGridView不起作用! [英] C# Insert and Update content to DataGridView do not work !

查看:125
本文介绍了C#插入和更新内容到DataGridView不起作用!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  // 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 + 
VALUES(+ temptabelas [0] +,+,$)(+$
(NArtista,NEditora,NGeneroDeMusica,NMedia,Nome,[Ano deEdição] + 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;

提前感谢
Luis Da Costa

解决方案

  Private Sub BoundLoadButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles BoundLoadButton.Click
swatch.Reset()
swatch.Start()
Cursor = Cursors.WaitCursor
尝试
使用BoundObject作为新的UnboundClass(mMySQLConnectionString)
调用BoundObject.BoundDataLoading(UnboundDataGridView,_
RecordCountTextBox,_
mErrorMsgString)
如果不是IsNothing(mErrorMsgString)然后
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)
结束尝试
Cursor = Cursors.Default
swatch.Stop()
mTimeDouble = swatch.ElapsedMilliseconds * 0.001
BoundTimeTextBox.Text = mTimeDouble.ToString
End Sub


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

解决方案

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

这篇关于C#插入和更新内容到DataGridView不起作用!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆