下面是数据库操作集锦,使用前先添加命名空间using System.Data.SqlClient;
////// 还回DataTable类型 /// /// SQL语句 ///public static DataTable ExcuteSQL(string sql) { DataTable dt = null; string connstring="server=localhost;database=DataBase;Uid=username;pwd=userpass"; using (SqlConnection sqlconn = new SqlConnection(connstring)) { sqlconn.Open(); SqlCommand slcom = sqlconn.CreateCommand(); slcom.CommandText = sql; SqlDataAdapter adap = new SqlDataAdapter(slcom); adap.Fill(dt); sqlconn.Close(); } return dt; } /// /// 返回Object类型 /// /// SQL语句 ///public static Object ExcuteSQLScalar(string sql) { Object ob = null; string connstring="server=localhost;database=DataBase;Uid=username;pwd=userpass"; using (SqlConnection sqlconn = new SqlConnection(connstring)) { sqlconn.Open(); SqlCommand slcom = sqlconn.CreateCommand(); slcom.CommandText = sql; ob = slcom.ExecuteScalar(); sqlconn.Close(); } return ob; } /// /// 不返回任何值 /// /// SQL语句 public static void SQLExcuteNone(string sql) { string connstring="server=localhost;database=DataBase;Uid=username;pwd=userpass"; using (SqlConnection sqlconn = new SqlConnection(connstring)) { sqlconn.Open(); SqlCommand slcom = sqlconn.CreateCommand(); slcom.CommandText = sql; slcom.ExecuteNonQuery(); sqlconn.Close(); } }