--AdminDal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; //包含Ado.Net的各类数据对象;
using System.Data.SqlClient; //包含访问SQL Server所需的各类对象;
using System.Configuration;
namespace 药品信息管理系统
{
public static class AdminDal
{
public static int SelectCount(Admin admin)
{
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ToString(); //配置管理器从App.config读取连接字符串;
SqlCommand sqlCommand1 = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
sqlCommand1.CommandText = "adm_selectAdminCount"; //指定SQL命令的命令文本;命令文本为存储过程名称;
sqlCommand1.CommandType = CommandType.StoredProcedure; //SQL命令的类型设为存储过程;
sqlCommand1.Parameters.AddWithValue("@ANo", admin.ANo); //向SQL命令的参数集合添加参数的名称、值;
sqlCommand1.Parameters.AddWithValue("@Pwd", admin.pwd);
sqlConnection.Open(); //打开SQL连接;
int adminCount = (int)sqlCommand1.ExecuteScalar(); //调用SQL命令的方法ExecuteScalar来执行命令,并接受单个结果(即标量);
sqlConnection.Close(); //关闭SQL连接;
return adminCount; //返回用户个数;
}
public static int Insert(Admin admin)
{
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ToString(); //配置管理器从App.config读取连接字符串;
SqlCommand sqlCommand = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
sqlCommand.CommandText = "admin_insertadmin"; //指定SQL命令的命令文本;命令文本为存储过程名称;
sqlCommand.CommandType = CommandType.StoredProcedure; //SQL命令的类型设为存储过程;
sqlCommand.Parameters.AddWithValue("@ANo", admin.ANo); //向SQL命令的参数集合添加参数的名称、值;
sqlCommand.Parameters.AddWithValue("@pwd", admin.pwd);
sqlCommand.Parameters.AddWithValue("@AName", admin.AName);
sqlCommand.Parameters.AddWithValue("@tel", admin.tel);
sqlConnection.Open(); //打开SQL连接;
int rowAffected = 0; //声明整型变量,用于保存受影响行数
try //尝试;
{
rowAffected = sqlCommand.ExecuteNonQuery(); //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
}
catch (SqlException sqlEx) //捕捉SQL异常;
{
if (sqlEx.Number == 2627) //若异常的编号为2627,则违反实体完整性,即插入了主键重复的记录;
{
admin.IsDuplicate = true; //存在雷同用户;
}
else
{
throw sqlEx;
}
}
return rowAffected; //返回受影响行数;
}
}
}
--ClientDal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; //包含访问SQL Server所需的各类对象;
using System.Configuration;
using System.Data;
namespace 药品信息管理系统
{
public class ClientDal
{
public static int Insert(Client client)
{
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ToString(); //配置管理器从App.config读取连接字符串;
SqlCommand sqlCommand = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
sqlCommand.CommandText = "cli_insertClient"; //指定SQL命令的命令文本;命令文本为存储过程名称;
sqlCommand.CommandType = CommandType.StoredProcedure; //SQL命令的类型设为存储过程;
sqlCommand.Parameters.AddWithValue("@CNo", client .CNo); //向SQL命令的参数集合添加参数的名称、值;
sqlCommand.Parameters.AddWithValue("@CName", client .CName);
sqlCommand.Parameters.AddWithValue("@CTel", client.CTel);
sqlCommand.Parameters.AddWithValue("@CAddress", client.CAddress);
sqlCommand.Parameters.AddWithValue("@sex", client.sex );
sqlCommand.Parameters.AddWithValue("@RegistDate", client.RegistDate );
sqlCommand.Parameters.AddWithValue("@Birthday", client.Birthday );
sqlCommand.Parameters.AddWithValue("@minsurance", client.minsurance );
//sqlCommand.Parameters.AddWithValue("@pinyin", client.pinyin );
sqlConnection.Open(); //打开SQL连接;
int rowAffected = 0; //声明整型变量,用于保存受影响行数
try //尝试;
{
rowAffected = sqlCommand.ExecuteNonQuery(); //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
}
catch (SqlException sqlEx) //捕捉SQL异常;
{
if (sqlEx.Number == 2627) //若异常的编号为2627,则违反实体完整性,即插入了主键重复的记录;
{
client.IsDuplicate = true; //存在雷同用户;
}
else
{
throw sqlEx;
}
}
return rowAffected; //返回受影响行数;
}
//public static SqlDataReader Select(Client client)
//{
//SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
//sqlConnection.ConnectionString =
// ConfigurationManager.ConnectionStrings["Sql"].ToString(); //配置管理器从App.config读取连接字符串;
//SqlCommand sqlCommand1 = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
//sqlCommand1.CommandText = "cli_selectClient"; //指定SQL命令的命令文本;命令文本为存储过程名称;
//sqlCommand1.CommandType = CommandType.StoredProcedure; //SQL命令的类型设为存储过程;
//sqlCommand1.Parameters.AddWithValue("@CNo", client .CNo ); //向SQL命令的参数集合添加参数的名称、值;
//sqlCommand1.Parameters.AddWithValue("@CName", client .CName );
//sqlCommand1.Parameters.AddWithValue("@CTel", client.CTel );
//sqlCommand1.Parameters.AddWithValue("@CAddress", client.CAddress );
//sqlConnection.Open(); //打开SQL连接;
//SqlDataReader sqlDataReader = sqlCommand1.ExecuteReader();
//while (sqlDataReader.Read())
//{
// string cno = sqlDataReader["@CNo"].ToString();
// string cname = sqlDataReader["@CName"].ToString();
// string ctel = sqlDataReader["@CTel"].ToString();
// string caddress = sqlDataReader["@CAddress"].ToString();
//}
//sqlDataReader.Close();
//sqlConnection.Close(); //关闭SQL连接;
//return sqlDataReader ; //返回用户个数;
// }
}
}
--SqlDBHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace 药品信息管理系统
{
class SqlDBHelper
{
public static string connString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
public static string ConnectionString
{
get { return connString; }
set { connString = value; }
}
}
}
原文地址:https://www.cnblogs.com/xxnzmy/p/12397606.html