简体   繁体   English

如何捕获空参数并允许在ADO.NET中使用DBNull进行解析

[英]How to capture a null Parameter and allow Parsing with DBNull in ADO.NET

So I am passing these 18 parameters to my DAL and then inserting or updating my Access database. 因此,我将这18个参数传递给DAL,然后插入或更新Access数据库。

I have 2 fields that are DateTime and one that is and int . 我有2个字段,分别是DateTime和一个和int Both DateTime and the int parameters are 'not required', which means that can be empty strings. DateTime和int参数均为“非必需”,这意味着可以为空字符串。

I've have a problem when I pass my txtBirthDate.Text, txtHireDate.Text, and txtReportsTo.Text that if they have been left null, my DAL screams at me with a FormatException that can't get past parsing a null element. 当我传递txtBirthDate.Text, txtHireDate.Text, and txtReportsTo.Text ,我遇到了一个问题txtBirthDate.Text, txtHireDate.Text, and txtReportsTo.Text即如果将它们保留为空,则DAL会以FormatException对我txtBirthDate.Text, txtHireDate.Text, and txtReportsTo.Text因为它无法解析null元素。

So my problem right now is, I'm unsure where to Parse the DateTime and int variable to allow my Parameters to accept them. 所以我现在的问题是,我不确定在哪里解析DateTimeint变量以允许我的Parameters接受它们。 I am also unsure how to set these variables to DBNull.Value that are have been past through to my DAL as empty strings. 我也不确定如何将这些变量设置为空字符串传递到我的DAL的DBNull.Value

Here is a sample of my DAL method call: 这是我的DAL方法调用的示例:

rowsAdded = ((DataAccessLayer)Application["dbAccess"]).insertEmployees(txtLname.Text, txtFname.Text, txtTitle.Text, 
         txtCourt.Text,txtBday.Text,txtHireDate.Text, txtAddress.Text, txtCity.Text,txtRegion.Text,txtPostalCode.Text,
         txtCountry.Text, txtHomePhone.Text, txtExtension.Text, upPhoto.FileName.ToString(),txtNotes.Text,txtReports.Text,txtPassword.Text);

Here is my DAL method: 这是我的DAL方法:

 public int insertEmployees(string lname, string fname, string title, string toc, string birth, string hire, string address, string city,
    string region,string postal,string country, string phone, string ext,string photo, string notes, string report, string pass)
{
    string last = lname;
    string first = fname;
    string tlt = title;
    string tOfc = toc;
    string addy = address;
    string town = city;
    string reg = country;
    string phum = phone;
    string exten = ext;
    string rep = report;// THIS IS Int
    string pas = pass;
    string pc = postal;
    string note = notes;
    string regions = region;
    string hD = hire;// THIS IS DATETIME
    string bD = birth;// THIS IS DATETIME
    string pho = photo;
    int rows = 0;
        dbString = "INSERT INTO [Employees] ([LastName],[FirstName],[Title],[TitleOfCourtesy],[BirthDate],[HireDate],[Address],[City],[Region],[PostalCode]," +
        "[Country],[HomePhone],[Extension],[Photo],[Notes],[ReportsTo],[Password]) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        string queryLast = "Select @@Identity";

        conn.Open();
        oleCommand = new OleDbCommand(dbString, conn);

        oleCommand.Parameters.Add(new OleDbParameter("@LastName", OleDbType.VarChar, 20, ParameterDirection.Input, false, 10, 0, "LastName", DataRowVersion.Original, null)).Value = last;
        oleCommand.Parameters.Add(new OleDbParameter("@FirstName", OleDbType.VarChar, 10, ParameterDirection.Input, false, 10, 0, "FirstName", DataRowVersion.Original, null)).Value = first;
        oleCommand.Parameters.Add(new OleDbParameter("@Title", OleDbType.VarChar, 30, ParameterDirection.Input, true, 10, 0, "Title", DataRowVersion.Original, null)).Value = tlt ?? (object)DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@TitleOfCourtesy", OleDbType.VarChar, 25, ParameterDirection.Input, true, 10, 0, "TitleOfCourtesy", DataRowVersion.Original, null)).Value = tOfc ?? (object)DBNull.Value;
//DATE  oleCommand.Parameters.Add(new OleDbParameter("@BirthDate", OleDbType.DBDate, 20, ParameterDirection.Input, true, 10, 0, "BirthDate", DataRowVersion.Original, null)).Value = (object)bD ?? DBNull.Value;
//DATE  oleCommand.Parameters.Add(new OleDbParameter("@HireDate", OleDbType.DBDate, 20, ParameterDirection.Input, true, 10, 0, "HireDate", DataRowVersion.Original, null)).Value = (object)hD ?? DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@Address", OleDbType.VarChar, 60, ParameterDirection.Input, true, 10, 0, "Address", DataRowVersion.Original, null)).Value = addy ?? (object)DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@City", OleDbType.VarChar, 15, ParameterDirection.Input, true, 10, 10, "City", DataRowVersion.Original, null)).Value = phum ?? (object)DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@Region", OleDbType.VarChar, 15, ParameterDirection.Input, true, 10, 0, "Region", DataRowVersion.Original, null)).Value = regions ?? (object)DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@PostalCode", OleDbType.VarChar, 10, ParameterDirection.Input, true, 10, 0, "PostalCode", DataRowVersion.Original, null)).Value = pc ?? (object)DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@Country", OleDbType.VarChar, 15, ParameterDirection.Input, true, 10, 0, "Country", DataRowVersion.Original, null)).Value = reg ?? (object)DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@HomePhone", OleDbType.VarChar, 24, ParameterDirection.Input, true, 10, 0, "HomePhone", DataRowVersion.Original, null)).Value = phum ?? (object)DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@Extension", OleDbType.VarChar, 4, ParameterDirection.Input, true, 10, 0, "Extension", DataRowVersion.Original, null)).Value = exten ?? (object)DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@Photo", OleDbType.VarChar, 255, ParameterDirection.Input, true, 10, 0, "Photo", DataRowVersion.Original, null)).Value = pho ?? (object)DBNull.Value; ;
        oleCommand.Parameters.Add(new OleDbParameter("@Notes", OleDbType.VarWChar, 255, ParameterDirection.Input, true, 10, 0, "Notes", DataRowVersion.Original, null)).Value = note ?? (object)DBNull.Value;
  //INT oleCommand.Parameters.Add(new OleDbParameter("@ReportsTo", OleDbType.Integer, 2, ParameterDirection.Input, true, 10, 0, "ReportsTo", DataRowVersion.Original, null)).Value = (object)rep ?? DBNull.Value;
        oleCommand.Parameters.Add(new OleDbParameter("@Password", OleDbType.VarChar, 255, ParameterDirection.Input, false, 10, 0, "Password", DataRowVersion.Original, null)).Value = pas ?? (object)DBNull.Value;
        rows = (int)oleCommand.ExecuteNonQuery();
        oleCommand.CommandText = queryLast;

for string values you can use this expression: 对于字符串值,可以使用以下表达式:

oleCommand.Parameters.Add(new OleDbParameter("@LastName", OleDbType.VarChar, 20, 
                                             ParameterDirection.Input, false, 10, 
                                             0, "LastName", 
                                             DataRowVersion.Original, null)
                         ).Value = String.IsNullOrEmpty(last) ? DBNull.Value : last;

Repeat it for the rest of string values 对其余的字符串值重复此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM