建立一个公共类SqlConnct,以后每次连接数据库进行查询操作时就调用这个类,调用时传入sql查询参数。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace WindowsFormsApplication3
{
class SqlConnect
{
public static DataSet sqlconn(string sql)
{
SqlConnection conn;
conn = new SqlConnection("server=.;database=schoolDB;uid=sa;pwd=111");
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter dsa = new SqlDataAdapter(sql, conn);
dsa.Fill(ds);
return ds;
}
}
}