服务器ip就把你的服务器地址写进去,端口应该是7726,用户名和密码写你建的用户名和密码,数据库写数据库全称,点sql链接测试一下。以下是代码示例
dim sqlcon As System.Data.SqlClient.SqlConnection
dim sqladapter As System.Data.SqlClient.SqlDataAdapter
dim ds As System.Data.DataSet
dim dr As System.Data.DataTable
sqlcon = New System.Data.SqlClient.SqlConnection
sqlcon.ConnectionString = "Data Source=127.0.0.1,7726; Initial Catalog=数据库 ; User ID=admin; Password=1234"
ds = New System.Data.DataSet
dr = New System.Data.DataTable("数据表")
ds.Tables.Add(dr)
sqladapter = New System.Data.SqlClient.SqlDataAdapter("select * from 数据表",sqlcon)
sqlcon.Open()
sqladapter.Fill(dr)
sqlcon.Close()
Dim conn As New ADODB.Connection, rs As New ADODB.Recordset
Private Sub Form_Load()
conn.ConnectionString = "Driver=;SERVER=ALLEN;DATABASE=你数据库的名字;UID=sa;PWD="
conn.Open()
End Sub
===============================
以上的是连接的代码
连接以后,每次运用都要先open,记得用完以后要close
代码如下:
conn.Open("Provider=SQLOLEDB;Data Source=ALLEN;UID=sa;PWD=")
下面就是你的SQL语句,如:sql="select * from test"
最后就是要关闭:conn.close()
'看看下面的代码吧
Public Function SQL_CONNECT(Server_Name, DataBase_Name, USER_ID, PWL) As ADODB.Connection
Set SQL_CONNECT = New ADODB.Connection
SQL_CONNECT.Open "Provider=SQLOLEDB;data Source=" & Server_Name & ";Initial Catalog=" & DataBase_Name & ";User Id=" & USER_ID & ";Password=" & PWL & ""
End Function
Sub Rs_Open(Record_Set_Name, Open_Str)
'If IsEmpty(Record_Set_Name) Then
Set Record_Set_Name = New ADODB.Recordset
'End If
If Record_Set_Name.State = 1 Then
Record_Set_Name.Close
End If
Record_Set_Name.CursorLocation = adUseClient
'Debug.Print Open_Str
Record_Set_Name.Open Open_Str, CN, adOpenKeyset, adLockOptimistic
Record_Set_Name.Filter = 0
'Record_Set_Name.MoveLast
'Record_Set_Name.MoveFirst
End Sub
把打印参数设置你设置的文本值
这样设计比较麻烦
用ODBC比较好
odbc