消息“错误”在vb.net 2015中使用新的sql命令时 [英] Message "error " when using new sql command in vb.net 2015

查看:59
本文介绍了消息“错误”在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


< p style ="text-autospace:none">        
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


< p style ="text-autospace:none">        
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 this

Imports System.Data

Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.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 com1 As SqlConnection

        com1 = New SqlConnection("data source = DESKTOP-T26PHBJ;initial catalog=latihan;integrated security=True")

    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

        Dim a As Integer

        a = DataGridView1.CurrentRow.Index

        With DataGridView1.Rows.Item(a)

            TextBox1.Text = .Cells(0).Value

            TextBox2.Text = .Cells(1).Value

            TextBox3.Text = .Cells(2).Value

        End With

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim com1 As SqlConnection

        com1 = New SqlConnection("data source = DESKTOP-T26PHBJ;initial catalog=latihan;integrated security=True")

        Dim a As Integer

        a = DataGridView1.CurrentRow.Index

        With DataGridView1.Rows.Item(a)

            .Cells(0).Value = TextBox1.Text

            .Cells(1).Value = TextBox2.Text

            .Cells(2).Value = TextBox3.Text

        End With

        Dim cmd As SqlCommand

        cmd = New SqlCommand("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)

        End With

        cmd.ExecuteNonQuery()

    End Sub

 But the the problem is 

cmd.ExecuteNonQuery()  show error

Please 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 names cmd = 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命令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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