Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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#,在SQL数据库中插入多行_C#_Sql_Ms Access - Fatal编程技术网

C#,在SQL数据库中插入多行

C#,在SQL数据库中插入多行,c#,sql,ms-access,C#,Sql,Ms Access,我有一个Access数据库。我正在使用C#和Visual Studio,试图在事务中插入几行。但是,我没有插入每一行,而是多次插入第一个元素 我可以将数据一次插入“发票”表中的一行,使该表处于工作状态。我把代码包括在下面。这些行确实有不同的数据。调试器显示了这一点 private void insertInvoiceItemsIntoDatabase(int tableNumber) { string category, food; double price string

我有一个Access数据库。我正在使用C#和Visual Studio,试图在事务中插入几行。但是,我没有插入每一行,而是多次插入第一个元素

我可以将数据一次插入“发票”表中的一行,使该表处于工作状态。我把代码包括在下面。这些行确实有不同的数据。调试器显示了这一点

private void insertInvoiceItemsIntoDatabase(int tableNumber)
{
    string category, food;
    double price
    string connectionString = _databaseConnectionString; 
    
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand();
        OleDbTransaction transaction = null;

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the transaction.
        try
        {
            connection.Open();

            // Start a local transaction with ReadCommitted isolation level.
            transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);

            command.Connection = connection;
            command.Transaction = transaction;

            command.CommandText = "Insert into Invoices (TableNumber, Category, Food, Price) VALUES (?,?,?,?)";

            command.CommandType = CommandType.Text;

            Order order;
            order = tables[tableNumber - 1].getOrder();
            List<MenuItem> items = order.deepCopyMenuItems();
            for (int i = 0; i < items.Count(); i++)
            {
                FoodType ft =  items[i].getFoodType();
                category = "Main Course";
                if(ft == FoodType.maincourse)
                {
                    category = "Main Course";
                }else if(ft == FoodType.drink)
                {
                    category = "Drink";
                }else if(ft == FoodType.dessert)
                {
                    category = "Dessert";
                }

                food = items[i].getItemName();
                price = items[i].getItemPrice();

                command.Parameters.AddWithValue("TableNumber", tableNumber);
                command.Parameters.AddWithValue("Category", category);
                command.Parameters.AddWithValue("Food", food);
                command.Parameters.AddWithValue("Price", price);
                command.ExecuteNonQuery();
            }
           
            transaction.Commit();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            try
            {
                // Attempt to roll back the transaction.
                transaction.Rollback();
            }
            catch
            {
                // Do nothing here; transaction is not active.
            }
        }
    }
}
private void insertinvoiceitemsintotatabase(int tableNumber)
{
串类,食品;
双倍价格
字符串连接字符串=_数据库连接字符串;
使用(OLEDB连接)=
新OLEDB连接(连接字符串))
{
OLEDBCOMAND命令=新的OLEDBCOMAND();
OleDbTransaction事务=null;
//将连接设置为新的OLEDB连接。
command.Connection=连接;
//打开连接并执行事务。
尝试
{
connection.Open();
//启动具有ReadCommitted隔离级别的本地事务。
事务=connection.BeginTransaction(IsolationLevel.ReadCommitted);
command.Connection=连接;
command.Transaction=事务;
command.CommandText=“插入发票(表号、类别、食品、价格)值(?、、?、?)”;
command.CommandType=CommandType.Text;
订单;
order=表[tableNumber-1].getOrder();
列表项=order.deepCopyMenuItems();
对于(int i=0;i
在循环之前创建具有适当类型的参数。在循环中设置值

        ...
        command.CommandText = "Insert into Invoices (TableNumber, Category, Food, Price) VALUES (?,?,?,?)";

        command.CommandType = CommandType.Text;
        command.Parameters.Add("TableNumber", /*param type*/);
        command.Parameters.Add("Category", /*param type*/);
        command.Parameters.Add("Food", /*param type*/);
        command.Parameters.Add("Price", /*param type*/);

        Order order;
        order = tables[tableNumber - 1].getOrder();
        List<MenuItem> items = order.deepCopyMenuItems();
        for (int i = 0; i < items.Count(); i++)
        {
            FoodType ft =  items[i].getFoodType();
            category = "Main Course";
            if(ft == FoodType.maincourse)
            {
                category = "Main Course";
            }else if(ft == FoodType.drink)
            {
                category = "Drink";
            }else if(ft == FoodType.dessert)
            {
                category = "Dessert";
            }

            food = items[i].getItemName();
            price = items[i].getItemPrice();

            command.Parameters["TableNumber"].Value = tableNumber;
            command.Parameters["Category"].Value =  category;
            command.Parameters["Food"].Value =  food;
            command.Parameters["Price"].Value =  price;
            command.ExecuteNonQuery();
        }
        ...
。。。
command.CommandText=“插入发票(表号、类别、食品、价格)值(?、、?、?)”;
command.CommandType=CommandType.Text;
command.Parameters.Add(“TableNumber”,/*param type*/);
command.Parameters.Add(“Category”、/*param type*/);
command.Parameters.Add(“Food”、/*param type*/);
command.Parameters.Add(“Price”、/*param type*/);
订单;
order=表[tableNumber-1].getOrder();
列表项=order.deepCopyMenuItems();
对于(int i=0;i
在循环之前创建具有适当类型的参数。在循环中设置值

        ...
        command.CommandText = "Insert into Invoices (TableNumber, Category, Food, Price) VALUES (?,?,?,?)";

        command.CommandType = CommandType.Text;
        command.Parameters.Add("TableNumber", /*param type*/);
        command.Parameters.Add("Category", /*param type*/);
        command.Parameters.Add("Food", /*param type*/);
        command.Parameters.Add("Price", /*param type*/);

        Order order;
        order = tables[tableNumber - 1].getOrder();
        List<MenuItem> items = order.deepCopyMenuItems();
        for (int i = 0; i < items.Count(); i++)
        {
            FoodType ft =  items[i].getFoodType();
            category = "Main Course";
            if(ft == FoodType.maincourse)
            {
                category = "Main Course";
            }else if(ft == FoodType.drink)
            {
                category = "Drink";
            }else if(ft == FoodType.dessert)
            {
                category = "Dessert";
            }

            food = items[i].getItemName();
            price = items[i].getItemPrice();

            command.Parameters["TableNumber"].Value = tableNumber;
            command.Parameters["Category"].Value =  category;
            command.Parameters["Food"].Value =  food;
            command.Parameters["Price"].Value =  price;
            command.ExecuteNonQuery();
        }
        ...
。。。
command.CommandText=“插入发票(表号、类别、食品、价格)值(?、、?、?)”;
command.CommandType=CommandType.Text;
command.Parameters.Add(“TableNumber”,/*param type*/);
command.Parameters.Add(“Category”、/*param type*/);
command.Parameters.Add(“Food”、/*param type*/);
command.Parameters.Add(“Price”、/*param type*/);
订单;
order=表[tableNumber-1].getOrder();
列表项=order.deepCopyMenuItems();
对于(int i=0;i
您只需使用一个命令,就可以一次又一次地添加相同的参数。只需在t的每次迭代中创建一个新的
OleDbCommand