这样吧,你新建一个工程,添加一个类,把下面的粘过去.
Imports System.Data.SqlClient
Public Class EditDataBase
Shared Function Insert(ByVal ConnectionString As String, ByVal SQLString As String) As Integer
Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
Dim Command As SqlCommand = New SqlCommand(SQLString, Connection)
Dim Transaction As SqlTransaction
Dim Count As Integer = 0
Command.Connection = Connection
Connection.Open()
Transaction = Connection.BeginTransaction()
Command.Transaction = Transaction
Try
Count = Command.ExecuteNonQuery()
()
Catch ex As SqlException
MsgBox(ex.ToString())
Catch ex As Exception
MsgBox(ex.ToString())
Finally
Connection.Close()
End Try
Return Count
End Function
Shared Function Delete(ByVal ConnectionString As String, ByVal table As String, ByVal row As String, ByVal value As String) As Integer
Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
Dim SQLString As String = "Delete Form" + table + "Where" + row + "=" + "" + value + ""
Dim Command As SqlCommand = New SqlCommand(SQLString, Connection)
Dim Count As Integer = 0
Try
Connection.Open()
Count = Command.ExecuteNonQuery()
Catch ex As SqlException
MsgBox(ex.ToString())
Finally
Connection.Close()
End Try
Return Count
End Function
Shared Function Update(ByVal ConnectionString As String, ByVal table As String, ByVal StrContent As String, ByVal row As String, ByVal value As String) As Integer
Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
Dim SQLString As String = "Update" + table + "Set" + StrContent + "Where" + row + "=" + "" + value + ""
Dim Command As SqlCommand = New SqlCommand(SQLString, Connection)
Dim Count As Integer = 0
Try
Connection.Open()
Count = Command.ExecuteNonQuery()
Catch ex As SqlException
MsgBox(ex.ToString())
Finally
Connection.Close()
End Try
Return Count
End Function
Shared Function Search(ByVal ConnectionString As String, ByVal SQLString As String) As DataTable
Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
Dim Command As SqlCommand = New SqlCommand(SQLString, Connection)
Connection.Open()
Dim Adapter As New SqlDataAdapter
Adapter.TableMappings.Add("Table", "TEMP")
Adapter.SelectCommand = Command
Dim ObjectdsDataSet As New DataSet()
Adapter.Fill(ObjectdsDataSet)
Connection.Close()
Return ObjectdsDataSet.Tables("TEMP")
End Function
End Class
然后添加一个表,加个按钮,加个DataGridView控件
添加一个新数据源,选择microsoft SQL server 数据库文件
随便创建一个表,里面有aaa,bbb,ccc这几个列
把数据库连接好,打开解决方案资源管理器中的My Project,打开设置,找到“值”那列的自动生成的连接字符串
粘贴到“按钮”的连接字符串中。
我的如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connAddStr As String
connAddStr = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\练习.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim SQLString = "INSERT INTO table1(aaa,bbb,ccc) VALUES('a1','b1','c1')"
Dim count As Integer = EditDataBase.Insert(connAddStr, SQLString)
SQLString = "insert into table1(aaa,bbb,ccc) values('a2','b2','c2')"
count = count + EditDataBase.Insert(connAddStr, SQLString)
Call EditDataBase.Insert(connAddStr, SQLString)
If (count > 0) Then
MsgBox("")
End If
End Sub
我就是这么做的,添加不了呀!