c#连接sql2000数据库(c# vs2005)

时间:2023-01-04 13:36:11

     /// <summary>

     /// 数据库连接

     /// </summary>

     /// <param name="sqlwhere">连接条件</param>

     /// <returns>结果数据集</returns>

     public static DataSet GetList(string sqlwhere)

 {

         //数据库连接字符

         String connectionString = "Server=localhost;Database=dbTest;uid=sa;pwd=sa;";

        //要执行的Sql语句

         String cmdText = "select * from table";

        if (sqlwhere != "")

        {

            cmdText += " where " + sqlwhere;

        }

        //建立连接

         SqlConnection conn = new SqlConnection(connectionString);

        //生成命名实例

         SqlCommand command = new SqlCommand(cmdText, conn);

        //生成数据集实例

         DataSet ds = new DataSet();

        //生成数据库连接实例

        SqlDataAdapter sqlDA = new SqlDataAdapter();

       //数据源中选择记录

        sqlDA.SelectCommand = command;

        //填充数据集

         sqlDA.Fill(ds);

        //关闭连接

        conn.Close();

        //返回数据集

        return ds; 

   }