连接sqlserver2008的问题 新人求教~~
用vb.net 连接sql2008 用的方法是 conn + DatAadapter + DataSet 的方法 现在实现读了 当时 写的时候发现无法更新 代码如下 请问怎么该Dim conn As New SqlConnection("Data Source=127.0.0.1;server=WIN-AFUESLL6BHL; Initial Catalog=CalTemp; user id=sa; password=King324586")
Dim adp As SqlDataAdapter = New SqlDataAdapter
Dim dtset As DataSet = New DataSet
adp.SelectCommand = New SqlCommand
adp.SelectCommand.Connection = conn
adp.SelectCommand.CommandText = "select * from Kingtest"
adp.SelectCommand.CommandType = CommandType.Text
conn.Open()
adp.Fill(dtset, "Kingtest")
dtset.Tables("Kingtest").Rows(1)(1) = 432
dtset.AcceptChanges()
adp.Update(dtset, "Kingtest")
MsgBox("success")
conn.Close() vb.net sql adapter dataset --------------------编程问答-------------------- 试试这个
vb.net操作SQL数据库比较好用的类。 .
Public Class ClassDB
Public cn As System.Data.SqlClient.SqlConnection
Public da As System.Data.SqlClient.SqlDataAdapter
Public ds As New DataSet
Public dt As New DataTable
Public cmd As System.Data.SqlClient.SqlCommand
'****************************************************************************************
'连接数据库
Public Function ConnectionOpen(ByVal servername As String, ByVal DBName As String, ByVal uid As String, ByVal pwd As String) As Boolean 'ByVal server As String, ByVal DBName As String, ByVal uid As String, ByVal datapwd As String
Dim cnstr As String = "Persist Security Info=False;" & "Server=" & servername & ";" & "Database=" & DBName & ";" & "User ID=" & uid & ";" & "Password=" & datapwd
cn = New System.Data.SqlClient.SqlConnection(cnstr)
Try
cn.Open()
Catch ex As System.Data.SqlClient.SqlException
MsgBox(ex.Message.ToString())
End Try
End Function
'断开数据库的连接
Public Function ConnectionClose() As Boolean
Try
cn.Close()
Catch ex As System.Data.SqlClient.SqlException
MsgBox(ex.Message.ToString())
End Try
End Function
'插入记录、删除记录、更新记录到指定的数据库->数据表
Public Function InsertDelUpdateRecord(ByVal cmdText As String) As Boolean
Try
cmd = New System.Data.SqlClient.SqlCommand(cmdText, cn) '指定sql命令及该命令用于哪一个数据库连接
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'填充数据集,他会根据cmdtext参数说指定的查询返回数据库里特定的表
Public Function DataSetFill(ByVal cmdText As String, ByVal tabName As String) As Boolean
Try
cmd = New System.Data.SqlClient.SqlCommand(cmdtext, cn) '指定sql查询命令及该命令用于哪一个数据库连接
da = New System.Data.SqlClient.SqlDataAdapter(cmd)
da.Fill(ds, tabName) '表名可以映射为数据库中实际的表名称,但是他和数据库实际的表名称不是一回事
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'调用存储过程
Public Function ExecProc(ByVal procName As String, ByVal tabName As String) As Boolean
Try
cmd = New System.Data.SqlClient.SqlCommand(procName, cn)
cmd.CommandType = CommandType.StoredProcedure
da = New System.Data.SqlClient.SqlDataAdapter(cmd)
da.Fill(ds, tabName)
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'这个基本不用,因为可以直接通过ds来调用
Public Function DataTableFill(ByVal index As Integer) As Boolean
Try
dt = ds.Tables(index)
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'这个基本不用,因为可以直接通过ds来调用
Public Function DataTableFill(ByVal name As String) As Boolean
Try
dt = ds.Tables(name)
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'释放资源
Public Function DBDispos() As Boolean
Try
cn.Dispose()
'da.Dispose()
ds.Dispose()
dt.Dispose()
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
End Class
示例 :
1、新建一个VS2008 VB.NET项目,并新建项(类),类名为ClassDB,打开这个类,删除里面的所有代码,并将以上的SQL连接类代码复制进去。
2、在工具箱中将数据控件DATAGRIDVIEW拖到表单中,表格名称为DataGrid1;
3、在表单的LOAD事件中写入以下代码:
Dim db As New ClassDB '新一个SQL连接类实例
db.ConnectionOpen("服务器机器名", "数据库名称", "数据库用户ID", "密码") '新建一个连接
db.DataSetFill("select bh, xm, csrq, xwhcd, jtmx, fzss, syzm, xq, qr, zr from DA_JBXX", "DA_JBXX") '填充数据集,参数一为SQL查询语句,第二参数为数据库中的表名。
DataGrid1.DataSource = db.ds.Tables(0) '绑定数据
db.ConnectionClose() '关闭连接
--------------------编程问答--------------------
Public cmd As System.Data.SqlClient.SqlCommand
改
Dim cmd As System.Data.SqlClient.SqlCommandBuilder
'没必要用 Public
--------------------编程问答-------------------- 谢谢,我非常需要这种连接方式,我试试看,如有不清楚的时侯再请教各位。 --------------------编程问答--------------------
只有 selectcommand 给了命令,updatecommand 还没指定, adp.update就无法得知如何更新了,要使用它更新,必须手动指定update语句;或者使用SqlCommandBuilder自动产生更新语句,表中需要有主键。
补充:.NET技术 , VB.NET