大家好:
    我刚学asp.net,想用C#编写连接SQL数据库的代码。我的服务器是wanglei-compute,数据源是datebase,数据库是mydb,表是info.
    哪位高手帮忙写一个连接代码,尽量写全!!

解决方案 »

  1.   

    SqlConnection con
    string conn;
    conn="workstation=服务器名称;user id=sa(这是登陆服务器的,可以不是);password=密码;data source=服务器名称;initial catalog=数据库名";
    con=new SqlConnection(conn);
    con.open();
      

  2.   

    //删除数据(整型主键)并返回实际删除的行数
    public int DeleteDatas(string tableName,string idColumnName,int id,params int[] otherId)
    {
    //构造删除SQL语句
    string whereSQL=" where "+idColumnName+" in ("+id.ToString();
    foreach(int other in otherId)
    {
    whereSQL=whereSQL+","+other.ToString();
    }
    whereSQL=whereSQL+")"; whereSQL="delete from "+tableName+whereSQL; //开始删除
    SqlConnection connection = new SqlConnection("Initial Catalog=mydb;Data Source=wanglei-compute;Packet Size=4096;user id=sa;password=");
    connection.Open();
    SqlCommand command = new SqlCommand(whereSQL, connection);
    int deleteRow=command.ExecuteNonQuery();
    //关闭连接
    if(connection!=null)
    connection.Close();
    return deleteRow;
    }
      

  3.   

    SqlConnection conn;
    string strConn;
    strConn="Server=wanglei-compute;DataBase=mydb;uid=登录SQLSERVER的用户名;pwd=登录密码";
    conn=new SqlConnection(strConn);
    conn.open();如果服务器是本机器,可以用. 或local代替服务器名
      

  4.   

    string strConn = "Provider=SQLOLEDB.1;Password=密码;Persist Security Info=True;User ID=用户名;Initial Catalog=mydb;Data Source=wanglei-compute";
    System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection(strConn);
    objConn.Open();