C# 在datetime列和integer列中插入空值
我正在使用C#并开发一个winform应用程序。我有一个项目类,它具有项目属性 项目类的构造函数如下所示:C# 在datetime列和integer列中插入空值,c#,sql,.net-3.5,C#,Sql,.net 3.5,我正在使用C#并开发一个winform应用程序。我有一个项目类,它具有项目属性 项目类的构造函数如下所示: newProject = new Project(GCD_ID.IsNull() ? (int?)null : Convert.ToInt32(GCD_ID), txt_Proj_Desc.Text, txt_Prop_Name.Text, ST.ID.ToString().IsNull() ? null: ST.ID.ToString(), cmbCentre.Text,
newProject = new Project(GCD_ID.IsNull() ? (int?)null : Convert.ToInt32(GCD_ID), txt_Proj_Desc.Text, txt_Prop_Name.Text, ST.ID.ToString().IsNull() ? null: ST.ID.ToString(), cmbCentre.Text,
SEC.ID.ToString().IsNull() ? null : SEC.ID.ToString(), cmbZone.Text,
FD.ID.ToString().IsNull() ? null : FD.ID.ToString(), DT.ID.ToString().IsNull() ? null : DT.ID.ToString(), OP.ID.ToString().IsNull() ? null : OP.ID.ToString(), T.ID.ToString().IsNull() ? null : T.ID.ToString(),
CKV.ID.ToString().IsNull() ? null : CKV.ID.ToString(), STAT.ID.ToString().IsNull() ? null : STAT.ID.ToString(), MW.IsNull() ? (Double?)null : Convert.ToDouble(MW),
txt_Subject.Text, Ip_Num.IsNull() ? (int?)null : Convert.ToInt32(Ip_Num), H1N_ID.IsNull() ? (int?)null : Convert.ToInt32(H1N_ID),
NOMS_Slip_Num.IsNull() ? (int?)null : Convert.ToInt32(NOMS_Slip_Num), NMS_Updated.IsNull() ? (DateTime?)null : Convert.ToDateTime(NMS_Updated),
Received_Date.IsNull() ? (DateTime?)null : Convert.ToDateTime(Received_Date), Actual_IS_Date.IsNull() ? (DateTime?)null : Convert.ToDateTime(Actual_IS_Date),
Scheduled_IS_Date.IsNull() ? (DateTime?)null : Convert.ToDateTime(Scheduled_IS_Date), UpSt.ID.ToString().IsNull() ? null : UpSt.ID.ToString(),
UpFd.ID.ToString().IsNull() ? null : UpFd.ID.ToString(), txtHVCircuit.Text, cmbbxSIA.Text);
我的问题是,当datetime变量和integer变量为null时,我无法将值插入数据库。所有这些数据都是从表单上的文本框分配给变量的
下面是数据库函数,它接收所有变量并将它们插入数据库
public static void createNewProject(int? GCD_ID, string Project_Desc, string Proponent_Name, int Station_ID, string OpCentre, int Sector_ID, string PLZone, int Feeder, int DxTx_ID, int OpControl_ID, int Type_ID, int ConnKV_ID, int Status_ID, double? MW, string Subject, int? Ip_Num, int? H1N_ID, int? NOMS_Slip_Num, DateTime? NMS_Updated, DateTime? Received_Date, DateTime? Actual_IS_Date, DateTime? Scheduled_IS_Date, int UP_Station_ID, int UP_Feeder_ID, string @HV_Circuit, string SIA_Required)
{
SqlConnection conn = null;
try
{
//Takes in all the employee details to be added to the database.
conn = new SqlConnection(databaseConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("createNewProject", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("GCD_ID", GCD_ID));
cmd.Parameters.Add(new SqlParameter("Project_Desc", MiscFunctions.Capitalize(Project_Desc)));
cmd.Parameters.Add(new SqlParameter("Proponent_Name", MiscFunctions.Capitalize(Proponent_Name)));
cmd.Parameters.Add(new SqlParameter("Station_ID", Station_ID));
cmd.Parameters.Add(new SqlParameter("OpCentre", OpCentre));
cmd.Parameters.Add(new SqlParameter("Sector_ID", Sector_ID));
cmd.Parameters.Add(new SqlParameter("PLZone", PLZone));
cmd.Parameters.Add(new SqlParameter("Feeder", Feeder));
cmd.Parameters.Add(new SqlParameter("DxTx_ID", DxTx_ID));
cmd.Parameters.Add(new SqlParameter("OpControl_ID", OpControl_ID));
cmd.Parameters.Add(new SqlParameter("Type_ID", Type_ID));
cmd.Parameters.Add(new SqlParameter("ConnKV_ID", ConnKV_ID));
cmd.Parameters.Add(new SqlParameter("Status_ID", Status_ID));
cmd.Parameters.Add(new SqlParameter("MW", MW));
cmd.Parameters.Add(new SqlParameter("Subject", Subject));
cmd.Parameters.Add(new SqlParameter("Ip_Num", Ip_Num));
cmd.Parameters.Add(new SqlParameter("H1N_ID", H1N_ID));
cmd.Parameters.Add(new SqlParameter("NOMS_Slip_Num", NOMS_Slip_Num));
cmd.Parameters.Add(new SqlParameter("NMS_Updated", NMS_Updated));
cmd.Parameters.Add(new SqlParameter("Received_Date", Received_Date));
cmd.Parameters.Add(new SqlParameter("Actual_IS_Date", Actual_IS_Date));
cmd.Parameters.Add(new SqlParameter("Scheduled_IS_Date", Scheduled_IS_Date));
cmd.Parameters.Add(new SqlParameter("UP_Station_ID", UP_Station_ID));
cmd.Parameters.Add(new SqlParameter("UP_Feeder_ID", UP_Feeder_ID));
cmd.Parameters.Add(new SqlParameter("HV_Circuit", HV_Circuit));
cmd.Parameters.Add(new SqlParameter("SIA_Required", SIA_Required));
cmd.ExecuteNonQuery();
}
catch (Exception e) //returns if error incurred.
{
MessageBox.Show("Error occured in createNewProject" + Environment.NewLine + e.ToString());
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
我的问题是,如何向数据库中插入值。
请帮助您需要检查该值是否为null,如果为null,则插入System.DBNull.value而不是该值 除此之外,我鼓励您创建一个结构(struct),将所有数据保存在单个数据类型中,而不是使用带有bajillion参数的函数签名。
您需要检查该值是否为null,如果为null,则插入System.DBNull.value而不是该值 除此之外,我鼓励您创建一个结构(struct),将所有数据保存在单个数据类型中,而不是使用带有bajillion参数的函数签名。
在插入数据库之前,您不能检查null,并使用一些合理的默认值吗
public static void createNewProject(int? GCD_ID........)
{
if(!GCD_ID.HasValue)
{
GCD_ID = 0;
}
.........
if(!Received_Date.HasValue)
{
Received_Date = DateTime.Today;
}
.......
}
PS:您是否考虑过将所有这些单独的值放入数据传输对象(例如,NewProjectDTO
),然后只传入该类的单个实例
public class NewProjectDTO
{
int GCD_ID { get; set; }
string Project_Desc { get; set; }
string Proponent_Name { get; set; }
....... (and so on)
}
public static void createNewProject(NewProjectDTO newProjectValues)
{
.....
}
一旦你有超过3, 4个参数,你就应该考虑将这些参数转换成某种类型的转移DTO!
此外,如果您有这样一个对象,您甚至可以向其添加一些逻辑,例如,在保存它之前,用合理的默认值替换空值….难道您不能在插入数据库之前检查空值,并改用一些合理的默认值吗
public static void createNewProject(int? GCD_ID........)
{
if(!GCD_ID.HasValue)
{
GCD_ID = 0;
}
.........
if(!Received_Date.HasValue)
{
Received_Date = DateTime.Today;
}
.......
}
PS:您是否考虑过将所有这些单独的值放入数据传输对象(例如,NewProjectDTO
),然后只传入该类的单个实例
public class NewProjectDTO
{
int GCD_ID { get; set; }
string Project_Desc { get; set; }
string Proponent_Name { get; set; }
....... (and so on)
}
public static void createNewProject(NewProjectDTO newProjectValues)
{
.....
}
一旦你有超过3, 4个参数,你就应该考虑将这些参数转换成某种类型的转移DTO!
此外,如果您有这样一个对象,您甚至可以向其添加一些逻辑,例如,在保存它之前用合理的默认值替换空值….您是否收到一个错误,表示缺少参数?如果是,对于每个具有空值的参数,请改用DBNull.value 因此,请这样做,或者做一些变化:
if(H1N_ID != null)
{
cmd.Parameters.Add(new SqlParameter("H1N_ID",HIN_ID);
}
else
{
cmd.Parameters.Add(new SqlParameter("H1N_ID",DBNull.Value);
}
此外,当您将这些字段从数据库读取到对象中时,应该检查DBNull.Value值并将其转换为null。是否收到一个错误,表示缺少参数?如果是,对于每个具有null值的参数,请改为使用DBNull.Value 因此,请这样做,或者做一些变化:
if(H1N_ID != null)
{
cmd.Parameters.Add(new SqlParameter("H1N_ID",HIN_ID);
}
else
{
cmd.Parameters.Add(new SqlParameter("H1N_ID",DBNull.Value);
}
此外,当您将这些字段从数据库读取到对象中时,您应该检查DBNull.Value值并将其转换为null。您确定您的数据库允许在插入的列中使用null值吗?如果不允许,则需要更改DB以支持null值或为每种类型插入默认值。是否确定您的数据库允许在插入的列中使用null值e您的数据库允许在您插入的列中使用空值?如果不允许,则需要更改数据库以支持空值或为每种类型插入默认值。我不想插入默认值…但是null@reggie-请看我的回答。我不想插入默认值…但是null@reggie-所以请看我的回答。