0

在我的 Windows 表单应用程序中,我有一个用户输入的表单,例如姓名地址......等等。

当用户按下添加条目时,所有输入条目将被添加到数据库中,同时,订单号将传递到另一个表单,在该表单中我有一个 SQL 查询来获取该特定订单的数据并分别填充它们文本框。

但是,当我将订单号从 form1 传递到 form2,并将该订单号传递给从数据库中检索数据的方法时,它显示数据库中没有这样的行,即使该条目已经在数据库中。如果我在表格 2 中手动硬编码订单号,它就可以工作。

那么如何在我将这些数据输入数据库后立即从数据库中检索数据。

基本上 form1 为用户提供订单号,而 form 2 使用该订单号来获取数据。发射顺序是先形式 1,然后是形式 2。

检索代码

Form1 m = new Form1();
string number = m.orderNumber();
//  string number = "ORD1012013";

string InvSql = "SELECT  (Customer.[Title] + SPACE(2)  + Customer.[Customer's Name]) as CustomerName, Customer.[Customer's Ebayname], Customer.[Email Address], Customer.[Phone Number], (Customer.[Address 1] + SPACE(2)  +Customer.[Address 2] + SPACE(2)  + Customer.[City] + SPACE(2)  + Customer.[Post Code]+  SPACE(2)  + Customer.[Country]) as Address, Customer.[Item Purchased], Customer.[Purchased Date], Customer.[Total Price], Customer.[OrderNumber] FROM Customer WHERE Customer.[OrderNumber]= '" + number + "'";

OleDbConnection cnn = new OleDbConnection(connString);
OleDbCommand cmdOrder = new OleDbCommand(InvSql, cnn);

cnn.Open();

OleDbDataReader rdrOrder = cmdOrder.ExecuteReader();

rdrOrder.Read();

custName.Text = rdrOrder["CustomerName"].ToString();
ebayName.Text = rdrOrder["Customer's Ebayname"].ToString();
email.Text = rdrOrder["Email Address"].ToString();
phone.Text = rdrOrder["Phone Number"].ToString();
address.Text = rdrOrder["Address"].ToString();
item.Text = rdrOrder["Item Purchased"].ToString();
date.Text = Convert.ToString(Convert.ToDateTime(rdrOrder["Purchased Date"]));
price.Text = rdrOrder["Total Price"].ToString();
order.Text = rdrOrder["OrderNumber"].ToString();

rdrOrder.Close();
cnn.Close();

添加条目的代码

private void button1_Click(object sender, EventArgs e)
        {
            foreach (Control c in this.Controls)
            {
                if (c is MaskedTextBox)
                {
                    MaskedTextBox textBox = c as MaskedTextBox;
                    if (textBox.Text == string.Empty)
                    {

                        string result = MyMessageBox.ShowBox("Please Enter All Fields", "Warning");
                    }

                    else
                    {
                        SaveAllListItems();
                        this.Close();
                        PrintOrder m = new PrintOrder();
                        m.Show();

                    }
                }
            }

对于 SaveAllListItems

 private void SaveAllListItems()
        {
            string listItems = string.Empty;
            foreach (var listBoxItem in listBox1.Items)
            {

                listItems += listBoxItem.ToString();

                if (listBox1.Items.IndexOf(listBoxItem) < listBox1.Items.Count - 1)
                {
                    listItems += ", ";
                }
            }

            InsertUser(maskedTextBox1.Text, comboBox1.Text, maskedTextBox2.Text, maskedTextBox3.Text, maskedTextBox4.Text, maskedTextBox5.Text,
                       maskedTextBox6.Text, maskedTextBox7.Text, maskedTextBox8.Text, maskedTextBox9.Text, listItems, DateTime.Now, maskedTextBox10.Text, orderNumber(), get_next_id());

;
        }

对于插入用户

public string InsertUser(string custName, string title, string cust,  string emailAddress, string phoneNumber, string address1, string address2, string city, string postCode, string country, string itemPurchased, DateTime datePurchased, string price,string orderNumber, int id)
{

    // Create connection objecte

    //string datePurchased = DateTime.Now.ToString("dd/MM/yyyy");


    int ix = 0;
    string rTurn = "";
    OleDbConnection oleConn = new OleDbConnection(connString);
    try
    {
        oleConn.Open();
        string sql = "INSERT INTO [Customer]([Customer's Ebayname], [Title],  [Customer's Name], [Email Address] ,[Phone Number], [Address 1], [Address 2], [City], [Post Code], [Country] , [Item Purchased], [Purchased Date], [Total Price], [OrderNumber], [NumGenerate])" +
                 "VALUES ( @custName, @title, @cust, @emailAddress, @phoneNumber, @address1, @address2, @city, @postCode, @country , @itemPurchased, @datePurchased, @price, @orderNumber, @id)";
        OleDbCommand oleComm = new OleDbCommand(sql, oleConn);

        oleComm.Parameters.Add("@custName", OleDbType.Char).Value = custName;
        oleComm.Parameters.Add("@title", OleDbType.Char).Value = title;
        oleComm.Parameters.Add("@cust", OleDbType.Char).Value = cust;
        oleComm.Parameters.Add("@emailAddress", OleDbType.Char).Value = emailAddress;
        oleComm.Parameters.Add("@phoneNumber", OleDbType.Char).Value = phoneNumber;
        oleComm.Parameters.Add("@address1", OleDbType.Char).Value = address1;
        oleComm.Parameters.Add("@address2", OleDbType.Char).Value = address2;
        oleComm.Parameters.Add("@city", OleDbType.Char).Value = city;
        oleComm.Parameters.Add("@postCode", OleDbType.Char).Value = postCode;
        oleComm.Parameters.Add("@country", OleDbType.Char).Value = country;
        oleComm.Parameters.Add("@itemPurchased", OleDbType.Char).Value = itemPurchased;
        oleComm.Parameters.Add("@datePurhcased", OleDbType.Date).Value = datePurchased;
        oleComm.Parameters.Add("@price", OleDbType.Char).Value = price;
        oleComm.Parameters.Add("@orderNumber", OleDbType.Char).Value = orderNumber;
        oleComm.Parameters.Add("@id", OleDbType.Integer).Value = id;



        ix = oleComm.ExecuteNonQuery();
        if (ix > 0)
            rTurn = "User Added";
        else
            rTurn = "Insert Failed";
    }
    catch (Exception ex)
    {
    }
    finally
    {
        oleConn.Close();
    }
    return rTurn;
    }

生成 OrderNumber

  public string orderNumber()
        {
           string ord = "ORD" + get_next_id() + DateTime.Now.Year;
           return ord;
       }
4

2 回答 2

1

看起来您m.orderNumber();在添加时调用并在检索时再次调用,并且在该方法中您正在调用get_next_id(). 因此,在从您保存的值中检索时,orderNumber 正在增加。(即保存订单#1,检索订单#2)

您可能应该将最后一个持久的订单 # 存储在静态属性中,或者将其直接传递给表单以进行检索。

于 2013-02-22T15:44:18.077 回答
0

您是否检查过 OrderNumber 字段的排序规则?这定义了如何在 SQL Server 中存储/比较文本。要检查的另一件事是字母的大小写。他们必须是大写/小写吗?

于 2013-02-22T15:26:21.223 回答