偶日常用的一些数据操作类
http://www.cnblogs.com/thcjp/archive/2006/06/18/428775.html也不知道算不算,反正我自己随时写的用,很粗浅的,适合跟我一样的新手收藏,指不定就用得上呢!一组是我这两天写留言本的(感谢第一控制的提醒,关于清理连接我回在做好之后再改正)
另组是以前VS2003写投票用的
高手看见的话,麻烦把好的解决方案说下,谢谢哦!!新手的话,共同学习了!!
--------下面是我最近写个留言本用的------------
public static OleDbConnection con()
{//数据库连接类
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["book"].ConnectionString);
return con;
}
public static bool insert(string que)
{ //根据传进来的SQL语句执行插入操作
OleDbConnection con = odb.con();
con.Open();
OleDbCommand cmd = new OleDbCommand(que,con);
int count = Convert.ToInt32(cmd.ExecuteNonQuery());
if (count > 0)
return true;
else
return false;
}
public static DataTable ds(string que)
{//返回一个装载了SQL制定留言的数据表,
OleDbConnection con = odb.con();
OleDbDataAdapter oda = new OleDbDataAdapter();
oda.SelectCommand=new OleDbCommand(que,con);
DataSet ds = new DataSet();
oda.Fill(ds,"thc");
return ds.Tables["thc"];
}
public static bool img(string que)
{//根据传来的条件查询该项是否有内容,有就返回true
OleDbConnection con = odb.con();
con.Open();
OleDbCommand cmd = new OleDbCommand(que,con);
if (cmd.ExecuteScalar().ToString() != "")
return true;
else
return false;
}
public static string scr(string que)
{//同样是根据传来的SQL语句返回一个字段的值,我不太喜欢把SQL语句做在内中,感觉不灵活
OleDbConnection con = odb.con();
con.Open();
OleDbCommand cmd = new OleDbCommand(que,con);
return cmd.ExecuteScalar().ToString();
}
-----------下面是以前2003的时候用的----------
public static SqlConnection con()
{//(静态)初始化链接,因为数据库路径在web.config文件里面,所以这里返回的是哪个字符串,这是VS2003的数据库连接初始实例
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
return con;
}
public static bool chklog(string name,string pwd)
{//判断数据库里是否存在这个用户,返回真或则假
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from admin where name='"+name+"' and pwd='"+pwd+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
return true;
}
else
{
return false;
}
}
public static string chkqx(string name,string pwd)
{//判断当前用户的权限,并返回权限
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select qx from admin where name='"+name+"' and pwd='"+pwd+"'",con);
string qx=Convert.ToString(cmd.ExecuteScalar());
return qx;
}
public static DataTable fill(string query)
{//根据传来的SQL语句查询出数据并填充一个表给查询对象
SqlConnection con=db.con();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand(query,con);
DataSet ds=new DataSet();
sda.Fill(ds,"vote");
return ds.Tables["vote"];
}
public static string title(int ID)
{//(静态)根据传来的ID返回该投票项标题
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select xiang from votemaster where id='"+ID+"'",con);
return cmd.ExecuteScalar().ToString();
}
public static void delete(string query)
{//(静态)执行指定的删除行为
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
cmd.ExecuteNonQuery();
}
public static void update(string query)
{//(静态)执行指定的更新行为
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
cmd.ExecuteNonQuery();
}
public static int cid(string query)
{//(静态)根据执行的语句查询出当前数据库最新的一个ID
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
int id=Convert.ToInt32(cmd.ExecuteScalar());
return id;
}
public static bool insert(string query)
{//插入操作
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
int count=Convert.ToInt32(cmd.ExecuteNonQuery());
if(count>0)
{
return true;
}
else
{
return false;
}
}
public static int typ(int id)
{//根据出来的ID,返回该ID所属于的投票属于多选还是单选
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select typ from votemaster where id='"+id+"'",con);
int typ=Convert.ToInt32(cmd.ExecuteScalar());
return typ;
}
public static string count()
{//返回总访问人数
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select coun from countline",con);
string count=cmd.ExecuteScalar().ToString();
return count;
}
public static string rengyi(string query)
{//查询任意单字段,返回字段值的一个静态方法
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
return cmd.ExecuteScalar().ToString();
}
public static bool vlog(string query)
{//查询任意单字段,返回真与假的一个静态方法
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
return true;
}
else
{
return false;
}
}
public static OleDbConnection con()
{//数据库连接类
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["book"].ConnectionString);
return con;
}
public static bool insert(string que)
{ //根据传进来的SQL语句执行插入操作
OleDbConnection con = odb.con();
con.Open();
OleDbCommand cmd = new OleDbCommand(que,con);
int count = Convert.ToInt32(cmd.ExecuteNonQuery());
if (count > 0)
return true;
else
return false;
}
public static DataTable ds(string que)
{//返回一个装载了SQL制定留言的数据表,
OleDbConnection con = odb.con();
OleDbDataAdapter oda = new OleDbDataAdapter();
oda.SelectCommand=new OleDbCommand(que,con);
DataSet ds = new DataSet();
oda.Fill(ds,"thc");
return ds.Tables["thc"];
}
public static bool img(string que)
{//根据传来的条件查询该项是否有内容,有就返回true
OleDbConnection con = odb.con();
con.Open();
OleDbCommand cmd = new OleDbCommand(que,con);
if (cmd.ExecuteScalar().ToString() != "")
return true;
else
return false;
}
public static string scr(string que)
{//同样是根据传来的SQL语句返回一个字段的值,我不太喜欢把SQL语句做在内中,感觉不灵活
OleDbConnection con = odb.con();
con.Open();
OleDbCommand cmd = new OleDbCommand(que,con);
return cmd.ExecuteScalar().ToString();
}
-----------下面是以前2003的时候用的----------
public static SqlConnection con()
{//(静态)初始化链接,因为数据库路径在web.config文件里面,所以这里返回的是哪个字符串,这是VS2003的数据库连接初始实例
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
return con;
}
public static bool chklog(string name,string pwd)
{//判断数据库里是否存在这个用户,返回真或则假
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from admin where name='"+name+"' and pwd='"+pwd+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
return true;
}
else
{
return false;
}
}
public static string chkqx(string name,string pwd)
{//判断当前用户的权限,并返回权限
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select qx from admin where name='"+name+"' and pwd='"+pwd+"'",con);
string qx=Convert.ToString(cmd.ExecuteScalar());
return qx;
}
public static DataTable fill(string query)
{//根据传来的SQL语句查询出数据并填充一个表给查询对象
SqlConnection con=db.con();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand(query,con);
DataSet ds=new DataSet();
sda.Fill(ds,"vote");
return ds.Tables["vote"];
}
public static string title(int ID)
{//(静态)根据传来的ID返回该投票项标题
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select xiang from votemaster where id='"+ID+"'",con);
return cmd.ExecuteScalar().ToString();
}
public static void delete(string query)
{//(静态)执行指定的删除行为
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
cmd.ExecuteNonQuery();
}
public static void update(string query)
{//(静态)执行指定的更新行为
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
cmd.ExecuteNonQuery();
}
public static int cid(string query)
{//(静态)根据执行的语句查询出当前数据库最新的一个ID
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
int id=Convert.ToInt32(cmd.ExecuteScalar());
return id;
}
public static bool insert(string query)
{//插入操作
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
int count=Convert.ToInt32(cmd.ExecuteNonQuery());
if(count>0)
{
return true;
}
else
{
return false;
}
}
public static int typ(int id)
{//根据出来的ID,返回该ID所属于的投票属于多选还是单选
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select typ from votemaster where id='"+id+"'",con);
int typ=Convert.ToInt32(cmd.ExecuteScalar());
return typ;
}
public static string count()
{//返回总访问人数
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand("select coun from countline",con);
string count=cmd.ExecuteScalar().ToString();
return count;
}
public static string rengyi(string query)
{//查询任意单字段,返回字段值的一个静态方法
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
return cmd.ExecuteScalar().ToString();
}
public static bool vlog(string query)
{//查询任意单字段,返回真与假的一个静态方法
SqlConnection con=db.con();
con.Open();
SqlCommand cmd=new SqlCommand(query,con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
return true;
}
else
{
return false;
}
}