public class DB
{
public static string ConnectionString//连接数据库字符串
{
get
{
string connectionString = "Data Source=服务器地址;Initial Catalog=数据库名称;Persist Security Info=True;User ID=数据库用户名;Password=数据库密码";
return connectionString;
}
}
public int ExecuteSql(string SQLString)//执行语句,执行成功后返回值>0
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connec();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch e)
{
connec();
throw e;
}
}
}
}
public bool Exist(DataSet ds)//判断数据集是否有值
{
if [0].Rows.Count > 0)
{
return true;
}
else
return false;
}
public bool DataReader(string SQLString)//用于快速查找,找到即返回ture
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
connec();
using (SqlDataReader dataReader = cmd.ExecuteReader())
{
try
{
return da();
}
catch e)
{
throw e;
}
}
}
}
}
public SqlDataAdapter command;
public DataSet Query(string SQLString)//返回一个数据集
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
DataSet ds = new DataSet();
try
{
connec();
command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch ex)
{
throw new Exception);
}
return ds;
}
}
}