How can I connect Visual Studio 2012 to SQL Server 2012 express on localhost. My server name :
HACEGAN\SQLEXPRESS
what must I write to my connection string ie
SqlConnection con = new SqlConnection("Data Source=localhost\\HACEGAN.SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123");
Just write your server name to your Data Source
part ?
Data Source=HACEGAN\SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123
Change your
localhost\\HACEGAN.SQLEXPRESS
to
HACEGAN\SQLEXPRESS
Check out: Visual Studio 2012 and MS Sql Server 2012 - connect with Server Explorer
You can find Server Explorer in Visual Studio 2012 -> View -> Server Explorer
我想你直接写
Data Source=HACEGAN\\SQLEXPRESS;Initial Catalog=Register;User ID=xxx;Password=xxx
You shouldn't be specifying both a servername & localhost. One or the other.
SqlConnection con = new SqlConnection(@"Data Source=HACEGAN\SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123");
Please note the @ symbol to prevent escaping issues.
If your sql server is also a default instance, you can use .
notation:
Server=.;Database=Register;User Id=sa;Password=123;
If non-default, use .\\SQLExpress
.
这篇文章中的所有其他答案都是正确的,但是对于您而言,您应该写:
SqlConnection con = new SqlConnection("Data Source=HACEGAN\\SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123");
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.