public class dbmanage
{
public dbmanage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection GetConnection()//建立数据库连接
{
SqlConnection cn=new SqlConnection();
cn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["conn"];
cn.Open();
return cn;
}
public void delid(int intId ,string strTable,SqlConnection cn)
{
SqlCommand DeleteCmd=new SqlCommand ("delete from "+strTable+" where id="+intId,cn);
DeleteCmd.ExecuteNonQuery();
}
public void ExecuteSql(string strSql,SqlConnection cn) //执行无返回值的sql命令
{
SqlCommand ExecuteCmd=new SqlCommand (strSql,cn);
ExecuteCmd.ExecuteNonQuery();
ExecuteCmd.Dispose();
}
public void UpdateTb(string strTbName, string strFieldsName,string strValue,int intId, SqlConnection cn)
{
SqlCommand UpdateCmd=new SqlCommand ("update strTbName set "+strFieldsName+"="+strValue+" where Id="+intId.ToString(),cn);
UpdateCmd.ExecuteNonQuery();
}
public DataSet FillDs(string strSql, SqlConnection cn)
{
SqlDataAdapter FillDsAdapter=new SqlDataAdapter(strSql,cn);
FillDsAdapter.SelectCommand.CommandType=CommandType.Text ;
DataSet ds =new DataSet();
FillDsAdapter.Fill(ds);
FillDsAdapter.Dispose();
return ds;
}
public DataSet FillDs(string strSql, SqlConnection cn,string srcTable)
{
SqlDataAdapter FillDsAdapter=new SqlDataAdapter(strSql,cn);
FillDsAdapter.SelectCommand.CommandType=CommandType.Text ;
DataSet ds =new DataSet();
FillDsAdapter.Fill(ds,srcTable);
FillDsAdapter.Dispose();
return ds;
}
public string GetString(string strSql,SqlConnection cn)//获取字符串
{
string strGetString="";
SqlDataAdapter FillDsAdapter=new SqlDataAdapter(strSql,cn);
FillDsAdapter.SelectCommand.CommandType=CommandType.Text ;
DataSet ds =new DataSet();
FillDsAdapter.Fill(ds);
if(ds.Tables[0].Rows.Count==1)
{
strGetString=ds.Tables[0].Rows[0][0].ToString();
}
else
{
strGetString=null;
}
FillDsAdapter.Dispose();
ds.Dispose();
return strGetString;
}
}