SqlCode(保存一下)估计老师就将这些啦

这篇文章转载自:阴立坤的QQ空间 [csharp] public class SqlCode { /// <summary> /// 创建连接字符串 /// </summary> /// <returns></returns> public static SqlConnection DBCon() { return new SqlConnection(@"server=JACK-PCSQLEXPRESS;database=test;uid=sa;pwd=jack123"); //Sql身份验证 //Integrated Security = SSPI/Windwos 身份验证 } /// <summary> /// 执行查询语句 /// </summary> /// <param name="SQLString">Sql查询语句(Select)</param> /// <returns></returns> public static DataSet Query(string SQLString) { using (SqlConnection conn = DBCon()) { DataSet ds = new DataSet(); try { conn.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, conn); command.Fill(ds, "ds"); command.Dispose(); conn.Close(); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds; }//当connection对象执行到这里时,即被释放,帮助内存回收 } /// <summary> /// 执行插入、删除、更新操作 /// </summary> /// <param name="Sql">包括 Insert、Delete、Update</param> public static void InOrDelOrUp(string Sql)//插入数据/删除数据/更新数据 { SqlConnection conn = DBCon(); conn.Open(); SqlCommand cmd = new SqlCommand(Sql, conn); cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); } /// <summary> /// 判断是否存在 /// </summary> /// <param name="Username">字符串</param> /// <returns></returns> public static bool CheckName(string Username) { SqlConnection conn = DBCon(); conn.Open(); SqlCommand cmd = new SqlCommand("select * from stu where sName="" + Username + """, conn); cmd.ExecuteNonQuery(); if (null != cmd.ExecuteScalar()) { cmd.Dispose(); conn.Close(); return false; } else { cmd.Dispose(); conn.Close(); return true; } } } [/csharp]