Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么它没有显示任何错误,也没有使用C将数据插入ms access文件?_C# - Fatal编程技术网

C# 为什么它没有显示任何错误,也没有使用C将数据插入ms access文件?

C# 为什么它没有显示任何错误,也没有使用C将数据插入ms access文件?,c#,C#,我是c语言的新手,我面临着这个问题。我做了两个按钮,一个用于箱子,另一个用于信用卡。当我点击按钮时,我的数据没有插入ms access文件。为什么它没有显示任何错误,我如何修复它 private void CashButton_Click(object sender, EventArgs e) { SaveOrder((int)PaymentTypes.Cash); } private void CreditCardButton_Click(object sender, EventAr

我是c语言的新手,我面临着这个问题。我做了两个按钮,一个用于箱子,另一个用于信用卡。当我点击按钮时,我的数据没有插入ms access文件。为什么它没有显示任何错误,我如何修复它

private void CashButton_Click(object sender, EventArgs e)
{
    SaveOrder((int)PaymentTypes.Cash);
}

private void CreditCardButton_Click(object sender, EventArgs e)
{
    SaveOrder((int)PaymentTypes.CreditCard);
}

private void SaveOrder(int paymentType)
{
try
{
    string connstring = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;

    using (OleDbConnection conn = new OleDbConnection(connstring))
    {
        conn.Open();

            using (OleDbCommand cmd = new OleDbCommand("INSERT INTO [Orders](OrderNummber,TransactionDate,ClientName,TotalAmount,PaymentType) VALUES(@OrderNummber,@TransactionDate,@ClientName,@TotalAmount,@PaymentType)",conn))
            {

                cmd.Parameters.AddWithValue("@OrderNummber",OrderNumberTextBox.Text);
                cmd.Parameters.AddWithValue("@TransactionDate",TransactionDateDateTimePicker.Value.Date);
                cmd.Parameters.AddWithValue("@ClientName",ClientNameTextBox.Text);
                cmd.Parameters.AddWithValue("@TotalAmount",Convert.ToDecimal(TotalAmountTextBox.Text));
                cmd.Parameters.AddWithValue("@PaymentType", paymentType);

                cmd.ExecuteNonQuery();

            }

                foreach (DataGridViewRow row in CartDataGridView.Rows)
                    { 
                        using (OleDbCommand cmd = new OleDbCommand("INSERT INTO [OrdersItems](OrderNumber,Quantity,UnitPrice,TotalPrice) VALUES(@OrderNumber,@Quantity,@UnitPrice,@TotalPrice)", conn))
                        {
                            cmd.Parameters.AddWithValue("@OrderNumber", OrderNumberTextBox.Text);
                            cmd.Parameters.AddWithValue("@Quantity", Convert.ToInt16(row.Cells["Quantity"].Value));
                            cmd.Parameters.AddWithValue("@UnitPrice",Convert.ToDecimal(row.Cells["UnitPrice"].Value));
                            cmd.Parameters.AddWithValue("@TotalPrice", Convert.ToDecimal(row.Cells["TotalPrice"].Value));

                            cmd.ExecuteNonQuery();
                        }
                    }
                        MessageBox.Show("Order is processed successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
}
catch (Exception ex)
{

}   
}像这样做

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void button2_Click(object sender, EventArgs e)
{

    OleDbConnection conn = new OleDbConnection();
    conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ryan\Desktop\Coding\Microsoft Access\Northwind.mdb";

    string fstName  = textBox1.Text.Trim();
    string lstName  = textBox2.Text.Trim();
    string adres = textBox3.Text.Trim();
    OleDbCommand cmd = new OleDbCommand(@"INSERT INTO MyExcelTable (FName, LName, Address) VALUES (@FName, @LName, @Address)")
    {
        Connection = conn
    };

    conn.Open();

    if (conn.State == ConnectionState.Open)
    {
        // you should always use parameterized queries to avoid SQL Injection
        cmd.Parameters.Add("@FName", OleDbType.VarChar).Value = fstName;
        cmd.Parameters.Add("@LName", OleDbType.VarChar).Value = lstName;
        cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = adres;

        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show(@"Data Added");
            conn.Close();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show(ex.Source + "\n" + ex.Message);
            conn.Close();
        }
    }
    else
    {
        MessageBox.Show(@"Connection Failed");
    }
}
   }
}

这肯定会奏效的。只需更改连接字符串和变量以满足您的需要。

这是因为您可以静默地捕获所有异常。空捕获对检测错误没有帮助。如果这是您的原始代码,我强烈建议正确的代码缩进,它的可读性可能要高得多。但它必须将数据插入access文件。除非您进入捕获,因为引发了异常,并且您没有注意到。在该catch中设置breakpint,或放置MessageBox.Showex.Message;