vb.net 想连接SQL server数据库 并能把text里的内容加到数据库里
代码是这样的:
Imports System.Data.SqlClient
Public Class FormJF
Dim objda As New SqlClient.SqlDataAdapter
Dim sqltable As New DataTable
Dim dataset11 As New DataSet
Private Sub FormJF_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Control.CheckForIllegalCrossThreadCalls = False
SerialPort1.Open()
LJJS()
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
TextBox1.Text = SerialPort1.ReadExisting
Dim myrow As DataRow = sqltable.NewRow
myrow("接收的数据") = TextBox1.Text
sqltable.Rows.Add(myrow)
objda.Update(dataset11, "JS")
sqltable.Clear()
End Sub
Public Sub LJJS()
Dim MyConnection As New SqlClient.SqlConnection
Dim sqldataadapter1 As New SqlClient.SqlDataAdapter
Dim sqlcomm As New SqlClient.SqlCommand
MyConnection.ConnectionString = "Data Source=L-FEI;Initial Catalog=date;Integrated Security=True"
sqlcomm.CommandText = "select * from JS"
sqlcomm.Connection = MyConnection
sqldataadapter1.SelectCommand = sqlcomm
MyConnection.Open()
sqldataadapter1.Fill(dataset11, "JS")
MyConnection.Close()
End Sub
End Class
添加数据时显示"接收的数据"不属于表
刚学习 希望大神们帮帮忙 谢谢
[解决办法]
Dim sqlbuilder As SqlCommandBuilder = New SqlCommandBuilder(objda)//追加
objda.Update(dataset11, "JS")
sqltable.Clear()
[解决办法]
例如:
SerialPort1_DataReceived{}
Dim objda As New SqlClient.SqlDataAdapter
Dim sqltable As New DataTable
objda = New SqlDataAdapter("select * from JS", myconn)
sqltable = New DataTable
objda.Fill(sqltable)
Dim myrow As DataRow = sqltable.NewRow
myrow("接收的数据") = TextBox1.Text
sqltable.Rows.Add(myrow)
Dim sqlbuilder As SqlCommandBuilder = New SqlCommandBuilder(objda)
objda.Update(sqltable, "JS")