C#在windows身份验证状态下,如何使用代码连接到SQL呢?

解决方案 »

  1.   

    在连接字符串中指明验证模式为SSPI:
    "Integrated Security=SSPI;database=...;server=...;Initial Catalog=..."
      

  2.   

    Data Source=数据库服务器IP;Initial Catalog=数据库名称;Integrated Security=True
      

  3.   


    string source= "server=服务器名;integrated security=SSPI;database=数据库名";
    SqlConnection conn = new SqlConnection(source);
      

  4.   

    web.config里
    <connectionStrings>
        <add name="tempdbConnectionString" connectionString="Data Source=192.168.91.169;Initial Catalog=meeting_Data;User ID=sa;Password=albbsxy123456" providerName="System.Data.SqlClient"/>
      </connectionStrings>
    代码
    string connectionString =ConfigurationManager.ConnectionStrings["tempdbConnectionString"].ConnectionString;
     using (SqlConnection con = connectionString )
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand(SQL语句, con);
                    cmd.ExecuteNonQuery();
      

  5.   

    Data Source=服务器名;Initial Catalog=库名;Integrated Security=True
      

  6.   

    <script language="C#" runat="server">
        protected void butsubmit_click(object sender, EventArgs e)
        {
           
            SqlConnection sqlconnection;
            string sqlconnstr = "Data Source=.;Initial Catalog=chapter;Integrated Security=True" ;
            sqlconnection=new SqlConnection(sqlconnstr);
            SqlCommand sqlcommand = new SqlCommand();
            sqlcommand.Connection = sqlconnection;
            sqlcommand.CommandText = "insert into ueserInfo (username,psw) values('" + this.username.Text + "','" +this. password.Text + "') ";
            int result = sqlcommand.ExecuteNonQuery();
            //sqlconnection.Close();
            if (result == 1)
            {
                Label2.Text="注册成功!";
                labcontent.Text = "您的用户名是 :" + username.Text;
                Label1.Text = "您的密码是 :" + password.Text;
            }
            else
            {
                Label2.Text = "注册失败!";
            }
         //   labcontent.Text = "您的用户名是 :"+username.Text;
          //  Label1.Text = "您的密码是 :"+password.Text;        
        }
    </script>
      

  7.   


    using (SqlConnection connection = new SqlConnection(connString))
    {
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    command.CommandType = CommandType.Text;
    command.CommandText = sql;
    command.Parameters.AddWithValue("@EditorId", EditorId);
    connection.Open();
    result = Convert.ToInt32(command.ExecuteScalar());
    connection.Close();
    }
    lz自己对着看把
      

  8.   

    ExecuteNonQuery 要求已打开且可用的连接。连接的当前状态为已关闭。说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: ExecuteNonQuery 要求已打开且可用的连接。连接的当前状态为已关闭。源错误: 
    行 18:         sqlcommand.Connection = sqlconnection;
    行 19:         sqlcommand.CommandText = "insert into ueserInfo (username,psw) values('" + this.username.Text + "','" +this. password.Text + "') ";
    行 20:         int result = sqlcommand.ExecuteNonQuery();
    行 21:         //sqlconnection.Close();
    行 22:         if (result == 1)
      

  9.   


     protected void butsubmit_click(object sender, EventArgs e)
      {
        
      int result=0;
      string sqlconnstr = "Data Source=.;Initial Catalog=chapter;Integrated Security=True" ;
      using(SqlConnection sqlconnection=new SqlConnection(sqlconnstr))
    {
      SqlCommand sqlcommand = new SqlCommand();
      sqlcommand.Connection = sqlconnection;
      sqlcommand.CommandText = "insert into ueserInfo (username,psw) values('" + this.username.Text + "','" +this. password.Text + "') ";
      result = sqlcommand.ExecuteNonQuery();
    }
      
      if (result == 1)
      {
      Label2.Text="注册成功!";
      labcontent.Text = "您的用户名是 :" + username.Text;
      Label1.Text = "您的密码是 :" + password.Text;
      }
      else
      {
      Label2.Text = "注册失败!";
      }
      // labcontent.Text = "您的用户名是 :"+username.Text;
      // Label1.Text = "您的密码是 :"+password.Text;    
      }
      

  10.   


    protected void butsubmit_click(object sender, EventArgs e)
      {
        
      int result=0;
      string sqlconnstr = "Data Source=.;Initial Catalog=chapter;Integrated Security=True" ;
      using(SqlConnection sqlconnection=new SqlConnection(sqlconnstr))
    {
      SqlCommand sqlcommand = new SqlCommand();
      sqlcommand.Connection = sqlconnection;
      sqlcommand.CommandText = "insert into ueserInfo (username,psw) values('" + this.username.Text + "','" +this. password.Text + "') ";
    sqlconnection.Open() //sorry 更正一下
      result = sqlcommand.ExecuteNonQuery();
    }
      
      if (result == 1)
      {
      Label2.Text="注册成功!";
      labcontent.Text = "您的用户名是 :" + username.Text;
      Label1.Text = "您的密码是 :" + password.Text;
      }
      else
      {
      Label2.Text = "注册失败!";
      }
      // labcontent.Text = "您的用户名是 :"+username.Text;
      // Label1.Text = "您的密码是 :"+password.Text;    
      }
      

  11.   

    http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.executenonquery(VS.80).aspx
      

  12.   

    让你看代码,你就是不看,你的sqlconnection打开了吗,没打开,你操作啥啊,
    sqlconnection.open();
      

  13.   

    Data Source=数据库服务器IP;Initial Catalog=数据库名称;Integrated Security=True