我有两个字段 DOB(出生日期)和(加入日期)如果用户输入日期,则在数据库中插入->日期,否则插入->空
我已将属性定义为
public DateTime DOB
{
get;
set;
}
public DateTime DOJ
{
get;
set;
}
现在在 save_click 事件中
IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
String datetime = txtDOB.Text.Trim();
DateTime date = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
objEmp.DOB = date;
provider = new System.Globalization.CultureInfo("en-CA", true);
datetime = txtDOJ.Text.Trim();
date = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
objEmp.DOJ = date;
类.cs
param[7] = new Service.SqlParameter();
param[7].ParameterName = "@DOB";
param[7].Value = DOB;
param[7].SqlDbType = Service.SqlDbType.DateTime;
param[8] = new Service.SqlParameter();
param[8].ParameterName = "@DOJ";
param[8].Value = DOJ;
param[8].SqlDbType = Service.SqlDbType.DateTime;
我希望当用户没有插入任何日期时,应该插入 null ......但是通过这段代码,我得到一个错误的 存储过程
@FK_EmployeeTypeID Numeric(18,0),
@EmployeeName NVARCHAR(50),
@CellNo NVARCHAR(50),
@PhNo NVARCHAR(50),
@Address NVARCHAR(Max),
@Email NVARCHAR(50),
@DOB DATETIME,
@DOJ DATETIME,
@DOR NVARCHAR(12),
@Status NVARCHAR(50),
@Remarks NVARCHAR(Max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
BEGIN TRY
BEGIN TRANSACTION Employee_Insert
INSERT
INTO
Employee
(
FK_EmployeeTypeID,
EmployeeName,
CellNo,
PhNo,
Address,
Email,
DOB,
DOJ,
DOR,
Status,
Remarks
)
VALUES
(
@FK_EmployeeTypeID,
@EmployeeName,
@CellNo,
@PhNo,
@Address,
@Email,
@DOB,
@DOJ,
@DOR,
@Status,
@Remarks
)
COMMIT TRANSACTION Employee_Insert
SELECT '1'
END TRY
BEGIN CATCH
SELECT 'Error : ' + ERROR_MESSAGE()
ROLLBACK TRANSACTION Employee_Insert
END CATCH
网络服务
[WebMethod]
public string InsUpdDel(string Conn, string ProcName, SqlParameter[] p)
{
try
{
using (SqlConnection cn = new SqlConnection(Conn))
{
if (cn.State == ConnectionState.Open || cn.State == ConnectionState.Broken || cn.State == ConnectionState.Connecting || cn.State == ConnectionState.Executing || cn.State == ConnectionState.Fetching)
cn.Close();
cn.Open();
SqlCommand cmd = new SqlCommand(ProcName, cn);
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter param in p)
{
cmd.Parameters.Add(param);
}
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr[0].ToString() == "1")
{
return "1";
}
else
{
return dr[0].ToString();
}
dr.Close();
cn.Close();
}
}
catch (Exception ex)
{
return "Error : " + ex.Message.ToString();
}