Posted in 面试题 onJuly 11, 2014
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();
}
}
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();
}
}
C#实现对任一张表的数据进行增,删,改,查要求,运用Webservice,体现出三层架构
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@