using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
namespace Coeno.CSManaweb
{
public class EmpCard
--定义EmpCard类
{
private static string _connectingString;
static EmpCard()
{
Initialize();
}
public static void Initialize()
{
string v_ConnectionStringsType = "HRCardConnectionString";
if(ConfigurationManager.ConnectionStrings[v_ConnectionStringsType]==null ||
ConfigurationManager.ConnectionStrings[v_ConnectionStringType].ConnectionString.Trim()=="")
{
throw new Exception("A connection string named 'ConnectionStringType' with a valid connection string "+"must exist in the <connectionStrings> configuration section for the application.");
}
_connectionString=ConfigurationManager.ConnectionStrings[v_ConnectionStringsType].ConnectionString;
}
public static void AddICCard(string v_EmpID,string v_EmpCardID,string v_EmpSerialNo,string v_CUser,out string v_ReturnStatus,out string v_ReturnID, out string ReturnMsg)
{
SqlConnection conn = new SqlConnection(_connectionString);
--初始化连接对象
SqlCommand comm = new SqlCommand("HR_CardID_GS_Mifare_Add", conn);
--sqlCommand
comm.CommandType = CommandType.StoredProcedure;
--类型为存储过程
comm.Parameters.Add("@EmpID", SqlDbType.NVarChar, 50).Value = v_EmpID;
--comm参数添加(变量名,自定义数据类型,长度)
comm.Parameters.Add("@EmpCardID", SqlDbType.NVarchar, 50).Value = v_EmpCardID;
comm.Parameters.Add("EmpSeriaNo", SqlDbType.NVarchar, 50).Value = v_EmpSerialNo;
comm.Parameters.Add("CUser", SqlDbType.NVarChar, 50).Value = v_CUser;
comm.Parameters.Add("@ReturnStatus", SqlDbType.NVarchar, 5);
comm.Parameters.Add("@ReturnID", SqlDbType.NVarchar, 10);
comm.Parameters.Add("@ReturnMsg", SqlDbType.NVarchar, 100);
comm.Parameters["@ReturnStatus"].Direction = ParameterDirection.Output;
--comm-@ReturnStatus参数接收存储过程的返回值
comm.Parameters["@ReturnID"].Direction = ParameterDirection.Output;
comm.Parameters["@ReturnMsg"].Direction = ParameterDirection.Output;
try
{
conn.Open();
comm.ExecuteNonQuery();
--ExecuteNonQuery方法主要用来更新数据。通常使用它来执行Update、Insert和Delete语句。
v_ReturnStatus = comm.Parameters["@ReturnStatus"].Value.ToString();
v_ReturnID = comm.Parameters["@ReturnID"].Value.ToString();
v_ReturnMsg = comm.Parameters["@ReturnMsg"].Value.ToString();
}
catch(SqlException ex)
{
v_ReturnStatus = "0";
v_ReturnID = "999";
v_ReturnMsg = "执行SP失败!(“ + ex.Message +”)";
}
finally
{
conn.Close();
}
}
}
}
注释:
Command对象通过ExecuteNonQuery方法更新数据库的过程,需要进行的步骤如下:
(1)创建数据库连接
(2)创建Command对象,并指定一个SQL insert、Update、Delete查询存储过程。
(3)把Command对象依附到数据库连接上。
(4)调用ExecuteNonQuery方法。
(5)关闭连接。