本文介绍了消息“错误”在vb.net 2015中使用新的sql命令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 亲爱的所有人, 到目前为止,我还没有使用表适配器的更新方法解决方案(VB.net 2015) ) 我 已经放了主键和另一个。 在这个我尝试使用新的sql连接,就像我的ciode这样 Imports System.Data Imports System.Data.SqlClient 公共 Class Form1 私人 Sub Form1_Load(发件人 As Object ,e As EventArgs ) 手柄 MyBase 。加载 'TODO:这行代码将数据加载到'LatihanDataSet.datamahasiswa'表中。您可以根据需要移动或删除它。 我 .DatamahasiswaTableAdapter.Fill( Me 。LatihanDataSet.datamahasiswa) Dim com1 As SqlConnection com1 = 新 SqlConnection ( " data source = DESKTOP-T26PHBJ ; initial catalog = latihan; integrated security = True" ) 结束 Sub 私人 Sub DataGridView1_CellContentClick(sender As Object ,e As DataGridViewCellEventArgs ) 手柄 DataGridView1.CellContentClick Dim a As 整数 a = DataGridView1.CurrentRow.Index DataGridView1.Rows.Item(a) TextBox1.Text = .Cells(0).Value TextBox2.Text = .Cells(1).Value TextBox3.Text = .Cells(2).Value 结束 使用 结束 Sub 私人 Sub Button1_Click(发件人 As Object ,e As EventArgs ) 手柄 Button1.Click Dim com1 As SqlConnection com1 = 新 SqlConnection ( " data source = DESKTOP-T26PHBJ ; initial catalog = latihan; integrated security = True" ) Dim a As 整数 a = DataGridView1.CurrentRow.Index DataGridView1.Rows.Item(a) .Cells(0).Value = TextBox1.Text .Cells(1).Value = TextBox2.Text .Cells(2).Value = TextBox3.Text 结束 Dim cmd As SqlCommand cmd = 新 SqlCommand ( " update datamahasiswa set no =' " & TextBox1.Text& "',nama ='" & TextBox2.Text& "',alamat ='" & ; TextBox3.Text& "',其中nama =' " & TextBox2.Text& "'" ,com1) cmd .Parameters.AddWithValue( " no" ,TextBox1.Text) .Parameters.AddWithValue( " nama" ,TextBox2.Text) .Parameters.AddWithValue( " alamat" ,TextBox3.Text) 结束 cmd.ExecuteNonQuery() 结束 Sub 但问题是 cmd.ExecuteNonQuery() 显示错误 请给我一个更正。 非常感谢你的时间和精力, 我最诚挚的问候, Muljanto 解决方案 你好, 'Dim cmd = New SqlCommand(" update datamahasiswa set no ='"& TextBox1.Text&"',nama ='"& TextBox2.Text&"',alamat ='"& TextBox3.Text& ;"',其中nama ='"& TextBox2.Text&"'",com1) '---> '这里你应该用参数名称替换值 cmd =新的SqlCommand("更新datamahasiswa set no = @ no,nama = @ nama,alamat = @ alamat,其中nama = @ nama",com1) '然后添加参数并传递它们的值使用cmd .Parameters.AddWithValue(" @ no",TextBox1.Text) .Parameters.AddWithValue(" @nama",TextBox2.Text) .Parameters.AddWithValue(" @@ alamat",TextBox3.Text) 以 '结束< --- 良好编码 Dear All,Until now , I have no solution in update method by using table adapter (VB.net 2015)I have put on primary key and the other.At this this I try to used new sql connection, like my ciode like thisImports System.DataImports System.Data.SqlClientPublicClassForm1 PrivateSub Form1_Load(senderAsObject, eAsEventArgs)HandlesMyBase.Load 'TODO: This line of code loads data into the 'LatihanDataSet.datamahasiswa' table. You can move, or remove it, as needed. Me.DatamahasiswaTableAdapter.Fill(Me.LatihanDataSet.datamahasiswa) Dim com1AsSqlConnection com1 = NewSqlConnection("data source = DESKTOP-T26PHBJ;initial catalog=latihan;integrated security=True") EndSub PrivateSub DataGridView1_CellContentClick(senderAsObject, eAsDataGridViewCellEventArgs)Handles DataGridView1.CellContentClick Dim aAsInteger a = DataGridView1.CurrentRow.Index With DataGridView1.Rows.Item(a) TextBox1.Text = .Cells(0).Value TextBox2.Text = .Cells(1).Value TextBox3.Text = .Cells(2).Value EndWith EndSub PrivateSub Button1_Click(senderAsObject, eAsEventArgs)Handles Button1.Click Dim com1AsSqlConnection com1 = NewSqlConnection("data source = DESKTOP-T26PHBJ;initial catalog=latihan;integrated security=True") Dim aAsInteger a = DataGridView1.CurrentRow.Index With DataGridView1.Rows.Item(a) .Cells(0).Value = TextBox1.Text .Cells(1).Value = TextBox2.Text .Cells(2).Value = TextBox3.Text EndWith Dim cmdAsSqlCommand cmd = NewSqlCommand("update datamahasiswa set no='" & TextBox1.Text &"',nama='" & TextBox2.Text &"',alamat='" & TextBox3.Text &"', where nama='" & TextBox2.Text &"'", com1) With cmd .Parameters.AddWithValue("no", TextBox1.Text) .Parameters.AddWithValue("nama", TextBox2.Text) .Parameters.AddWithValue("alamat", TextBox3.Text) EndWith cmd.ExecuteNonQuery() EndSub But the the problem is cmd.ExecuteNonQuery() show errorPlease give me the correction one.Thanks so much for your time and attention,My best regards,Muljanto 解决方案 hello, 'Dim cmd = New SqlCommand("update datamahasiswa set no='" & TextBox1.Text & "',nama='" & TextBox2.Text & "',alamat='" & TextBox3.Text & "', where nama='" & TextBox2.Text & "'", com1)' --->'here you should replace values with parameters namescmd = New SqlCommand("update datamahasiswa set no=@no,nama=@nama,alamat=@alamat, where nama=@nama", com1)' and then add parameters and pass their values With cmd .Parameters.AddWithValue("@no", TextBox1.Text) .Parameters.AddWithValue("@nama", TextBox2.Text) .Parameters.AddWithValue("@alamat", TextBox3.Text) End With' <---Good Coding 这篇关于消息“错误”在vb.net 2015中使用新的sql命令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 15:12