【教程】SqlServer数据库连接操作
vb.net吧
全部回复
仅看楼主
level 2
佚名软件 楼主
'连接到服务器(本机)
Public Const ServerConnStr As String = _
"Server=(Local)\SQLEXPRESS;" & _
"DataBase=;" & _
"Integrated Security=SSPI"
'连接到服务器(远程)
Public Const ServerConnStr As String = _
"Server=SevrerName;" & _
"DataBase=DataBaseName;" & _
"User ID=UserName;" & _
"Password=PassWord"
'连接到数据库
Public Const SqlConnStr As String = _
"Server=(Local)\SQLEXPRESS;" & _
"DataBase=DataBaseName;" & _
"Integrated Security=SSPI"
'连接到数据库指定端口
Public Const SqlConnStr As String = _
"Server=(Local);" & _
"Port=1433;" & _
"DataBase=DataBaseName;" & _
"User ID=UserName;" & _
"Password=PassWord"
'=========================对查询返回表的处理========================================
Try
Dim SQLCmdStr As String = "SELECT * FROM TableName WHERE ColumnName='StrVar'"
Dim SQLConn As New SqlConnection(SQLConnStr)
Dim SQLCmd As New SqlCommand(SQLCmdStr, SQLConn)
SQLConn.Open()
Dim SQLAdp As New SqlDataAdapter(SQLCmd)
SQLConn.Close()
Dim TempDataSet As New DataSet
SQLAdp.Fill(TempDataSet)
If TempDataSet.Tables(0).Rows.Count > 0 Then
'your code
End If
Catch sqlExc As SqlException
MsgBox(sqlExc.Message, MsgBoxStyle.OkOnly, "SQL Error")
Catch Exc As Exception
MsgBox(Exc.Message, MsgBoxStyle.OkOnly, "Application Error")
End Try
'=========================不需要返回值的操作========================================
Try '添加信息到数据库 Dim SQLCmdStr As String = "INSERT INTO TableName(ColumnName) VALUES('ValueStr')" '更新信息到数据库 Dim SQLCmdStr As String = "UPDATE TableName SET UpdateColumnName='UpdateStrVar' WHERE WhereColumName='WhereStrVar'" '从数据库删除信息 Dim SQLCmdStr As String = "DELETE FROM TableName WHERE WhereColumName ='WhereStrVar'" Dim SQLConn As New SqlConnection(SQLConnStr) Dim SQLCmd As New SqlCommand(SQLCmdStr, SQLConn) SQLConn.Open() SQLCmd.ExecuteNonQuery() SQLConn.Close() Catch sqlExc As SqlException MsgBox(sqlExc.Message, MsgBoxStyle.OkOnly, "SQL Error") Catch Exc As Exception MsgBox(Exc.Message, MsgBoxStyle.OkOnly, "Application Error") End Try
2016年08月22日 08点08分 1
level 2
佚名软件 楼主
'连接到服务器(本机)
Public Const ServerConnStr As String = _
"Server=(Local)\SQLEXPRESS;" & _
"DataBase=;" & _
"Integrated Security=SSPI"
'连接到服务器(远程)
Public Const ServerConnStr As String = _
"Server=SevrerName;" & _
"DataBase=DataBaseName;" & _
"User ID=UserName;" & _
"Password=PassWord"
'连接到数据库
Public Const SqlConnStr As String = _
"Server=(Local)\SQLEXPRESS;" & _
"DataBase=DataBaseName;" & _
"Integrated Security=SSPI"
'连接到数据库指定端口
Public Const SqlConnStr As String = _
"Server=(Local);" & _
"Port=1433;" & _
"DataBase=DataBaseName;" & _
"User ID=UserName;" & _
"Password=PassWord"
'=========================对查询返回表的处理========================================
Try
Dim SQLCmdStr As String = "SELECT * FROM TableName WHERE ColumnName='StrVar'"
Dim SQLConn As New SqlConnection(SQLConnStr)
Dim SQLCmd As New SqlCommand(SQLCmdStr, SQLConn)
SQLConn.Open()
Dim SQLAdp As New SqlDataAdapter(SQLCmd)
SQLConn.Close()
Dim TempDataSet As New DataSet
SQLAdp.Fill(TempDataSet)
If TempDataSet.Tables(0).Rows.Count > 0 Then
'your code
End If
Catch sqlExc As SqlException
MsgBox(sqlExc.Message, MsgBoxStyle.OkOnly, "SQL Error")
Catch Exc As Exception
MsgBox(Exc.Message, MsgBoxStyle.OkOnly, "Application Error")
End Try
'=========================不需要返回值的操作========================================
Try
'添加信息到数据库
Dim SQLCmdStr As String = "INSERT INTO TableName(ColumnName) VALUES('ValueStr')"
'更新信息到数据库
Dim SQLCmdStr As String = "UPDATE TableName SET UpdateColumnName='UpdateStrVar' WHERE WhereColumName='WhereStrVar'"
'从数据库删除信息
Dim SQLCmdStr As String = "DELETE FROM TableName WHERE WhereColumName ='WhereStrVar'"
Dim SQLConn As New SqlConnection(SQLConnStr)
Dim SQLCmd As New SqlCommand(SQLCmdStr, SQLConn)
SQLConn.Open()
SQLCmd.ExecuteNonQuery()
SQLConn.Close()
Catch sqlExc As SqlException
MsgBox(sqlExc.Message, MsgBoxStyle.OkOnly, "SQL Error")
Catch Exc As Exception
MsgBox(Exc.Message, MsgBoxStyle.OkOnly, "Application Error")
End Try
2016年08月22日 08点08分 2
1