C#教务系统namespace xitong
{
public class DataAccess
{
public String connstr; //连接字符串
SqlConnection conn; // 连接对象
SqlCommand cmd; //命名对象
SqlDataAdapter da;//数据适配器
public String getconn() //定义一个连接字符串的方法
{
connstr = @"data source=.\SQLEXPRESS;initial catalog=sscggl;integrated security=true;";
return connstr;
}
public bool excutesql(string sql) //返回影响的行数
{
try
{
conn = new SqlConnection(getconn());
conn.Open();
}
catch
{
MessageBox.Show("数据连接失败", "温馨提示");
}
try
{
cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
// conn.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作失败");
return false;
}
finally
{
conn.Close();
}
}
public DataSet getdataset(string sql, string tablename) //返回数据集
{
DataSet ds = new DataSet(); //定义一个数据集
conn = new SqlConnection(getconn()); //连接对象
conn.Open(); //打开连接
try
{
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, tablename);
//conn.Close();
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
conn.Close();
2020-01-09 03:07:23
21.71MB
C#教务系统
1