string sCnnStr="server=172.16.1.111;uid=username;pwd=password;database=数据库名;";
数据库连接字符创
Cnn=new SqlConnection(sCnn);
Cnn.Open();

解决方案 »

  1.   

    string sCnnStr="server=服务器名;uid=用户名;pwd=密码;database=数据库名";

    SqlConnection Cn=new SqlConnection();
    Cn.ConnectionString=sCnnStr;
    Cnn.Open();、记得在顶部引用一下  using System.Data.SqlClient;
      

  2.   

    附:登录窗体的制作
    新建一个类:DbConnection.cs
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient ;
    namespace TFSS
    {
    /// <summary>
    /// DbConnection 的摘要说明。
    /// </summary>
    public class DbConnection
    {
    private string ConnectString;
    public SqlDataReader myReader; public DbConnection()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //

    }
    public DbConnection(string a)
    {
       ConnectString=a;
       SqlConnection conn=new SqlConnection ("server=hyg;uid=sa;pwd=;database=TFSS");
       conn.Open ();
       SqlCommand cmd=new SqlCommand (ConnectString,conn);
       cmd.CommandType=CommandType.Text ;
       myReader=cmd.ExecuteReader() ;
    }
    }
    }
    在web窗体中 Login.aspx
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    namespace TFSS
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button cmdOk;
    protected System.Web.UI.WebControls.Button cmdCancel;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.TextBox txtUsername;
    protected System.Web.UI.WebControls.TextBox txtPassword;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.Label Label2;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.cmdOk.Click += new System.EventHandler(this.cmdOk_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void cmdOk_Click(object sender, System.EventArgs e)
    {
    string uid;
    string pwd;
    uid=txtUsername.Text ;
    pwd=txtPassword.Text ;
    string connstr;
    connstr="select *from userinfo where username='"+uid+"' and password='"+pwd+"'";
    DbConnection obj=new DbConnection (connstr);
    //SqlConnection conn=new SqlConnection ("server=hyg;uid=sa;pwd=;database=TFSS");
    //conn.Open ();
                //SqlCommand cmd=new SqlCommand ("select *from userinfo where username='"+uid+"' and password='"+pwd+"'",conn);
    //SqlDataReader myReader;
    //cmd.CommandType=CommandType.Text ;
    //myReader=cmd.ExecuteReader() ;
    if(obj.myReader.Read())
    {

    Response.Redirect ("main.htm");
    }
    else
    {
       Response.Write ("不能识别的用户名和官码!");
    } }
    }
    }