简体   繁体   English

SQL Server无法建立与db的连接字符串

[英]SQL Server cannot build connection string to db

I tried the following in SQL Server Management Studio; 我在SQL Server Management Studio中尝试了以下方法; I can connect and run query, but not from my project 我可以连接并运行查询,但不能从我的项目中运行

string s = "Server=LAPTOP-7J47C5JA\SQLEXPRESS;Database=Test;Trusted_Connection=True;";

string queryString =   "SELECT * from tbclienti";

cn = new SQLConnection(s);

// Create the Command and Parameter objects.
SqlCommand command = new SqlCommand(queryString, cn);

cn.Open();

SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
    Text = reader[0].ToString();
    label1.Text = reader[1].ToString()+ " " + reader[2].ToString();
}

reader.Close();
cn.Close();

I posted here whole code what could look like. 我在这里发布了整个代码,看起来像什么。 Try it now. 现在就试试。

    string s = @"Server=(local)\SQLEXPRESS;Database=Test;Integrated Security=SSPI;";

    using(SqlConnection cn = new SqlConnection(s))
    {
        string queryString = "SELECT * from tbclienti";

        try
        {    
          cn.Open();
          // Create the Command and Parameter objects.
          SqlCommand command = new SqlCommand(queryString, cn);
          SqlDataReader reader = command.ExecuteReader();
          while (reader.Read())
          {
             Text = reader[0].ToString();
             label1.Text = reader[1].ToString()+ " " + reader[2].ToString();

           }
           reader.Close();
         }
         catch(Exception ex) 
         {
             // see if error appear
         }
         finally
         {
           cn.Close();
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM