0

Visual C# 2008 速成版。访问 2010 数据库。当我使用以下代码时,我收到一个错误“条件表达式中的数据类型不匹配”,但如果我取出 txt85x11bw_Validating 事件的代码,它就可以正常工作。我试图了解日期类型不匹配的原因。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace GISReciepts
{
public partial class GISReceipts : Form
{
    public GISReceipts()
    {
        InitializeComponent();
    }

    private void cmdExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void cmdAdd_Click(object sender, EventArgs e)
    {
        string strSQL = "INSERT INTO Table1(Name1, Date1, CollectedBy, 85x11BW) VALUES(@Name, @Date, @CollectedBy, @85x11BW)";

        //OleDbConnection is a class that represents an open connection to a data source
        OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Temp\\GISTest.accdb");
        //OleDbCommand is a class that represents an SQL statement or stored procedure to execute against a data source.(takes care of passing queries to the database.
        OleDbCommand myCommand = new OleDbCommand(strSQL, myConnection);
        myCommand.Parameters.AddWithValue("@Name", txtName.Text);
        myCommand.Parameters.AddWithValue("@Date", maskedTextBoxDate.Text);
        myCommand.Parameters.AddWithValue("@CollectedBy", txtCollectedBy.Text);
        myCommand.Parameters.AddWithValue("@85x11BW", txt85x11bw.Text);

        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            myConnection.Close();
           // MessageBox.Show("Entry Added");  //this is displaying even on errors & when no data is added to table
        }

    }

    private void GISReceipts_Load(object sender, EventArgs e)
    {
        maskedTextBoxDate.Text = DateTime.Today.ToString("MM/dd/yyyy");

        txt85x11bw.Validating += new CancelEventHandler(txt85x11bw_Validating);       
    }


    private void GISReceipts_FormClosing(object sender, FormClosingEventArgs e)
    {
        DateTime value;
        //DateTime.TryParse converts the srting representation of a date to its DateTime equivalent & returns a value that indicates whether the conversion succedded.
        if (!DateTime.TryParse(maskedTextBoxDate.Text, out value))
        {
            maskedTextBoxDate.Text = DateTime.Today.ToShortDateString();
        }

    }

    private void txt85x11bw_Validating(object sender, CancelEventArgs e)
    {
        if (string.IsNullOrEmpty(txt85x11bw.Text))
        {
            //do nothing
        }
        else
        {
            //initialize the variable numberEntered of type int
            int numberEntered;

            //int=integer TryParse=method that converts strings into integer (value, out result)
            if (int.TryParse(txt85x11bw.Text, out numberEntered))
            {
                //if number is less than 1 or more than 1,000
                if (numberEntered < 1 || numberEntered > 1000)
                    MessageBox.Show("You must enter a valid number");
                txt85x11bw.Clear();

            }
            //if conversion failed
            else
            {
                MessageBox.Show("You must enter a number");
                txt85x11bw.Clear();

            }
        }


    }

}

}

4

1 回答 1

0

这一行添加了一个数据类型为 text 的参数,相反,我假设在您的数据库中,列 85x11BW 是数字类型(一个 int 查看您的验证代码)

    myCommand.Parameters.AddWithValue("@85x11BW", txt85x11bw.Text);

尝试以这种方式更改线路

    myCommand.Parameters.AddWithValue("@85x11BW", Convert.ToInt32(txt85x11bw.Text));

通过这种方式,您可以强制 AddWithValue 方法添加一个参数,并将其DbType属性设置为Int32. @Date如果相对数据表列属于DateTime类型,则参数可能会遇到相同的问题。

编辑如果 txt85x11BW 为空,您需要决定为数据表中的该列接受哪个默认值。如果你接受一个 NULL 你可以写这样的东西

        int numberEntered = 0;
        int.TryParse(txt85x11bw.Text, out numberEntered);
        if (numberEntered >= 1 && numberEntered <= 1000)
             myCommand.Parameters.AddWithValue("@85x11BW", numberEntered);
        else
             myCommand.Parameters.AddWithValue("@85x11BW", DBNull.Value);

编辑 2 在验证事件中,如果值正确,代码也会清除 maskedit 的内容。条件后需要大括号

if (int.TryParse(txt85x11bw.Text, out numberEntered))
{
    //if number is less than 1 or more than 1,000
    if (numberEntered < 1 || numberEntered > 1000)
    {
        MessageBox.Show("You must enter a valid number");
        txt85x11bw.Clear();
    }
}
于 2013-01-31T15:26:14.310 回答