0

所以我将这 18 个参数传递给我的 DAL,然后插入或更新我的 Access 数据库。

我有 2 个字段,DateTime一个是 and int。DateTime 和 int 参数都不是必需的,这意味着可以是空字符串。

当我通过我的时候遇到了一个问题txtBirthDate.Text, txtHireDate.Text, and txtReportsTo.Text,如果它们被保留为空,我的 DAL 会用FormatException无法通过解析空元素的方式向我尖叫。

所以我现在的问题是,我不确定在哪里解析DateTimeandint变量以允许我的参数接受它们。我也不确定如何将这些变量设置DBNull.Value为已通过我的 DAL 作为空字符串传递的变量。

这是我的 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);

这是我的 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;
4

1 回答 1

0

对于字符串值,您可以使用此表达式:

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;

对其余的字符串值重复此操作

于 2012-11-25T22:26:34.480 回答