连接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;
}

posted @ 2014-06-04 20:16  BicycleBoy  阅读(402)  评论(0编辑  收藏  举报