I am trying to learn to connect to and perform functions with SQL Server database using C# (out of ASP.NET website). When I try to run
SqlConnection con = new SqlConnection("Data Source=.; database=fengshuidb; integrated security = SSPI");
SqlCommand cmd = new SqlCommand("Select * from emails", con);
con.Open();
to connect I get the following error with the con.Open()
instruction.
From VS:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
What do I need to do to configure connection to database? What security settings do I need? Are there any good resources for this topic?
Try entering the Connection this way
SqlConnection myConnection = new SqlConnection("Database=fengshuidb;Server=[Your_PC_Name]\\SQLEXPRESS;Integrated Security=True;connect timeout = 30");
Where "[Your_PC_Name]" is the name of you local machine if the database is local. Also take a look at this link: http://msdn.microsoft.com/en-us/library/jj653752%28v=vs.110%29.aspx
尝试使用SqlConnectionStringBuilder创建有效的连接字符串
You missed Initial Catalog in your connection part.
Change it to this:
Data Source=Your sql connection ;Initial Catalog=Your Database name;Integrated Security=True
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.