[英]Connection to sql server express with visual studio 2010
I'm new to .net and c# and want to connect Sql server express available in visual studio 2010. 我是.net和C#的新手,并且希望连接Visual Studio 2010中可用的Sql Server Express。
I have searched a lot and read connection strings in connectionstrings.com but still don't know how to connect. 我已经搜索了很多内容,并在connectionstrings.com中阅读了连接字符串,但仍然不知道如何连接。
i added a "service-based database" with adding new item and now have a db.mdf with tables. 我添加了一个添加新项的“基于服务的数据库”,现在有一个带有表的db.mdf 。
my code: 我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class First
{
static void Main()
{
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=db2.mdf;Integrated Security=True");
con.Open();
Console.WriteLine("ok");
Console.ReadLine();
}
}
}
but it does not connect. 但它没有连接。 in fact it executes with no errors , but no output for Console.WriteLine("ok"); 实际上,它执行没有错误,但没有Console.WriteLine(“ ok”);的输出。
how should be the connection-string in my case? 在我的情况下,连接字符串应该如何? specially for "Data source". 专门用于“数据源”。 I don't know what should its value be. 我不知道它的价值是多少。 please tell me the full connection-string which suits my case. 请告诉我适合我情况的完整连接字符串。
I'm really new to .net and confused many days for a simple connection. 我真的是.net的新手,为简单的连接而困惑了很多天。
also tried many other connection strings , but not working... 也尝试了许多其他连接字符串,但不起作用...
try with this code 尝试使用此代码
using(var connection = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db2.mdf;Integrated Security=True;User Instance=True")
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.