2012-11-18 167 views
1

有人可以帮我修理我的连接字符串吗?我是使用MS SQL Management Studio的绝对初学者,但我是一位经验丰富的C#程序员。我想弄清楚如何连接到我的电脑上的本地数据库。我今天刚刚安装了SQL Server 2012 Express,并创建了一行包含一行数据的表。我正在尝试从C#程序访问该表。我一直在寻找帮助来调用一个存储过程(没有参数),它看起来像我一切正常,但我得到一个异常错误“无法找到存储过程'GetCustomers'”。我也尝试将我的过程名称更改为“dbo.GetCustomers”以及“SqlTest.dbo.GetCustomers”和“SqlTest.GetCustomers”,但似乎没有任何工作。很明显,我没有正确连接到我的数据库。我已经为此工作了4个小时,因此我该停下来寻找帮助。我认为我需要的只是一个很好的连接字符串和程序的正确语法。如何从C#程序连接到本地Microsoft Sql server 2012 Express数据库?

 Connect c = new Connect(); 
     if(c.MakeConnection()) 
     { 
      try 
      { 
       DataSet data = new DataSet(); 
       SqlDataAdapter adaptor = new SqlDataAdapter(); 

       //changed to use stored procedure 
       adaptor.SelectCommand = new SqlCommand("GetCustomers", c.MyConnect); 
       adaptor.SelectCommand.CommandType = CommandType.StoredProcedure; 
       //adaptor.SelectCommand.ExecuteNonQuery();//this throws an exception. 
       adaptor.Fill(data);//this throws an exception. 
      } 
      catch (Exception e) 
      { 
       Logger.WriteMessage(e.Message); 
      } 
      finally 
      { 
       c.CloseConnection(); 
      } 

我的连接类包含以下内容:我曾尝试

string connection = Properties.Settings.Default.DatabaseConnectString; 
sqlConnection = new SqlConnection(connection); 
sqlConnection.Open(); 

连接字符串似乎连接OK其中:

Server=(localdb)\v11.0;Trusted_Connection=Yes; 
Server=(localdb)\v11.0;Integrated Security=true; 

我的数据库名称是sqltest语句。我已经在连接字符串中尝试了几个变体,但其中大多数都会抛出登录失败的异常错误。我验证了我的Windows用户ID具有数据库的管理员权限。

连接字符串我已经试过这cive我登录错误:

Server=(localdb)\v11.0;Initial Catalog=SqlTest;User ID=Raphael\couchpotato;Integrated Security=SSPI;Trusted_Connection=Yes;   
Server=(localdb)\v11.0;Initial Catalog=dbo;User ID=Raphael\couchpotato;Integrated Security=SSPI;Trusted_Connection=Yes; 
Server=(localdb)\v11.0;Database=SqlTest;Trusted_Connection=Yes; 
Server=(localdb)\v11.0;Database=SqlTest;Integrated Security=true; 

回答

1

我想我只需要睡一会儿。 ;-)

我需要将我的所有SQL服务器服务设置为自动。出于某种原因,他们被设置为手动,所以他们没有开始。

然后,我还需要在连接字符串中设置正确的服务器名称。这与启动SQL Server Management Studio时用于登录的服务器名称相同。这是连接并访问正确的数据库和表的连接字符串:

Server=RAPHAEL\SQLEXPRESS;Database=SqlTest;Trusted_Connection=Yes; 
相关问题