0

したがって、これらの18個のパラメーターをDALに渡し、Accessデータベースを挿入または更新します。

私には2つのフィールドがありDateTime、1つはとですint。DateTimeパラメータとintパラメータはどちらも「不要」です。つまり、空の文字列にすることができます。

txtBirthDate.Text, txtHireDate.Text, and txtReportsTo.Textそれらがnullのままになっていると、私のDALがFormatExceptionnull要素の解析を通過できないと私に叫ぶという問題があります。

したがって、現在の私の問題は、パラメータがそれらを受け入れることができるように、変数を解析する場所がわからないことDateTimeですint。また、これらの変数を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 に答える