C#执行sql语句

//SQL查询语句
  public DataTable query(string sql)
  {
   DataTable dt=new DataTable();
   string connString = "server=IP;uid=sa;pwd=;database=数据库名";
   SqlConnection conn = new SqlConnection(connString);
   SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);
   adapter.Fill(dt);
   return dt;
  }
//SQL非查询语句
  public void non_query(string sql)
  {
   string connString = "server=IP;uid=sa;pwd=;database=数据库名";
   SqlConnection conn = new SqlConnection(connString);
   SqlCommand cmd = new SqlCommand(sql,conn);
   conn.Open();
   cmd.ExecuteNonQuery();
   conn.Close(); 
  }

 

你可能感兴趣的:(C#)