C#实现对任一张表的数据进行增,删,改,查要求,运用Webservice,体现出三层架构
1.下面是M层的,其他层的自己写
using System.Data.SqlClient;
public class data
{
private SqlConnection conn;//定义一个数据库连接
private SqlCommand com;//定义执行命令
public data()
{
//在配置文件中设置
conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.AppSettings["connString"].ToString();
}
//打开数据库
public void open()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
}
//关闭数据库
public void close()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
//执行数据库操作,返回DataSet
public void getDataSet(string proc,SqlParameter[] parm,out DataSet ds)
{
com = getCommand(proc,parm);
SqlDataAdapter sda = new SqlDataAdapter(com);
ds = new DataSet();
sda.Fill(ds);
}
//返回SqlCommadn
public SqlCommand getCommand(string proc, SqlParameter[] parm)
{
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = proc;
foreach (SqlParameter p in parm)
{
com.Parameters.Add(p);
}
return com;
}
//返回DataReader
public SqlDataReader getDataReader(string proc, SqlParameter[] parm)
{
com = getCommand(proc, parm);
return? com.ExecuteReader();
}
//执行数据库操作,返回void
public void ExecuteCommand(string proc, SqlParameter[] parm)
{
com = getCommand(proc, parm);
open();
com.ExecuteNonQuery();
close();
}
}