问题描述
我试图从很多天连接到mssql服务器,我也在google搜索了很多次但是我连续无法使用连接字符串连接到sql server。
当我使用ms sql连接时服务器工作室然后它工作正常,但当我使用连接字符串连接到mssql服务器,然后它无法连接。我注册的网站为我提供了这个连接字符串
I am trying to connect to mssql server from many days and i searched in google also many times but continuously i am failing to connect to sql server using connection string.
When i connect using ms sql server studio then it works fine but when i connect to mssql server using connection string then it fails to connect. Website where i am registerd provided me this connection string
"workstation id=chattha.mssql.somee.com;packet size=4096;user id=rizwan;pwd=censored;data source=chattha.mssql.somee.com;persist security info=False;initial catalog=chattha".
我试过我的代码中的这些连接字符串
I tried these connection strings in my code
con.Open( _
"Provider = sqloledb;" & _
"Data Source=mssql.chattha.somee.com;" & _
"Initial Catalog=chattha;" & _
"User ID=rizwan;" & _
"Password=censored;")
我也试过这个连接字符串
I also tried this connection string also
con.Open("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=rizwan;Password=censored;Initial Catalog=chattha;Data Source=chattha.mssql.somee.com")
以上所有我尝试过,但他们中的任何一个都没有工作。
我尝试下面的代码,但它也没有用。
Above all i tried but any of them is not working.
I tried below code also but it also did not worked.
Dim dbconn, sql, dbcomm, dbread
'dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("AddressBook.mdb"))
'dbconn = New OleDbConnection("Provider = sqloledb;user id=rizwan;pwd=censored;data source=chattha.mssql.somee.com;persist security info=False;initial catalog=chattha")
dbconn = New OleDbConnection("Data Source=chattha.mssql.somee.com ;Initial Catalog='chattha';USE='rizwan';PWD='censored'")
dbconn.Open()
sql = "SELECT * FROM Table1"
dbcomm = New OleDbCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader()
Table1.DataSource = dbread
Table1.DataBind()
dbread.Close()
dbconn.Close()
对于实验我添加了gridview并设置了sqlDataSource,它工作正常并正确显示gridview中的记录。但是我没有在代码中使用连接字符串。
注意:我添加了这个引用并使用它Microsoft ActiveX Data Objects Library 6.1 Library。
请帮助我。
For experiment i added gridview and set sqlDataSource and it works fine and show records in gridview correctly. But i am failing to use connection string in code.
Note: I added this reference and working with it "Microsoft ActiveX Data Objects Library 6.1 Library.
Please help me.
推荐答案
con.Open("Provider=SQLOLEDB.1;Persist Security Info=False;User ID=rizwan;Password=censored;Initial Catalog=chattha;Data Source=chattha.mssql.somee.com,1433")
同时确保防火墙允许使用正确的端口进行通信。
Also ensure that communication using the correct port is allowed in the firewall.
这篇关于连接字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!