首页 > 代码库 > 连接SQL Server执行SQL语句
连接SQL Server执行SQL语句
public static DataTable GetData()
{
string Connect = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection sc = new SqlConnection(Connect);
sc.Open();
SqlCommand cm = new SqlCommand("select * from Department");
cm.Connection = sc;
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cm);
sda.Fill(ds, "abc");
DataTable dt = ds.Tables["abc"];
return dt;
}
public static string ExecSQL(string sql)
{
string Connect = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection sc = new SqlConnection(Connect);
sc.Open();
SqlCommand cm = new SqlCommand(sql);
cm.Connection = sc;
string str = cm.ExecuteNonQuery().ToString();
sc.Close();
return str;
}