C#08.03.10 数据库连接
来源:互联网 发布:网络侦探巴尔巴兽 编辑:程序博客网 时间:2024/05/29 10:00
public DataSet GetEmployee()
{
//创建连接
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString;
connection.Open();
//创建命令
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText="SELECT [EmployeeID], [LastName], [FirstName], [Title], [City] FROM [Employees]";
//获取DataSet
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
//关闭连接
connection.Close();
//返回数据
return ds;
}
public DataSet GetEmployee(string lastname)
{
//创建连接
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString;
connection.Open();
//创建命令
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT [EmployeeID], [LastName], [FirstName], [Title], [City] FROM [Employees] WHERE [lastname] LIKE '%'+@lastname+'%'";
//command.Parameters["@lastname"].Value=lastname;
command.Parameters.AddWithValue("@lastname",lastname);
//获取DataSet
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
//关闭连接
connection.Close();
//返回数据
return ds;
}
DAL:
public class DataAccess
{
public DataAccess(string connectionstring)
{
connection = new SqlConnection();
connection.ConnectionString = connectionstring;
command = new SqlCommand();
command.Connection = connection;
}
public void AddWithValue(string name,string value)
{
command.Parameters.AddWithValue(name,value);
}
public DataSet GetDataSet(string cmdtext)
{
command.CommandText = cmdtext;
SqlDataAdapter adapter = new SqlDataAdapter(command);
connection.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);
connection.Close();
return ds;
}
SqlConnection connection;
SqlCommand command;
}
public DataSet GetEmployee()
{
DataAccess da=new DataAccess(ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString);
return da.GetDataSet("SELECT [EmployeeID], [LastName], [FirstName], [Title], [City] FROM [Employees]");
}
public DataSet GetEmployee(string lastname)
{
DataAccess da=new DataAccess(ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString);
da.AddWithValue("@lastname", lastname);
return da.GetDataSet("SELECT [EmployeeID], [LastName], [FirstName], [Title], [City] FROM [Employees] WHERE [lastname] LIKE '%'+@lastname+'%'");
}
BLL:
public DataSet GetEmployee()
{
EmployeeDAL dal = new EmployeeDAL();
return dal.GetEmployee();
}
public DataSet GetEmployee(string lastname)
{
EmployeeDAL dal = new EmployeeDAL();
return dal.GetEmployee(lastname);
}
- C#08.03.10 数据库连接
- C#数据库连接
- c#数据库连接
- C#数据库连接
- c# 数据库连接
- C# 数据库连接
- C# 数据库连接
- C# 数据库连接
- C# 数据库连接
- c#数据库连接
- C#数据库连接
- C#数据库连接
- C# ~ 数据库连接
- c# 数据库连接
- C#数据库连接字符大全
- C#数据库连接字符大全
- C# - Oracle数据库连接方法
- c# SQL数据库连接
- java NIO
- 浮点数的存储与计算
- Statement和PreparedStatement之间的区别
- JNI入门
- vc中的xml开发
- C#08.03.10 数据库连接
- 基于UML的食堂就餐卡系统的设计
- 继承,多态,重载和重写的区别。
- 如何查看oracle用户权限
- TSS(Task-State Segment)任务状态段
- 商业智能(BI)是一种解决方案
- Linux下的Memcache安装
- 返过头来重新看java基础知识
- 用UML模型实现大型实时监控应用软件