Search
 
SCRIPT & CODE EXAMPLE
 

SQL

mysql insert into select transaction c#

const string InsertStatement1 = @"INSERT INTO tbl_inv (cname, date, duedate, subtotal, vat, total) 
    VALUES (@cname, @date, @duedate, @subtotal, @vat, @total);";
const string InsertStatement2 = @"INSERT INTO tbl_line (invno, pname, qty, unitprice, amount) 
    VALUES (@invno, @pname, @qty, @unitprice, @amount);";

cn.Open();
using (MySqlTransaction sqlTrans = cn.BeginTransaction())
using (MySqlCommand sqlCommand = new MySqlCommand(InsertStatement1, cn, sqlTrans))
{
    sqlCommand.Parameters.Add("@cname", comboBox1.Text);
    sqlCommand.Parameters.Add("@date", dateTimePicker1.Value.ToString("yyyy-MM-dd"));
    sqlCommand.Parameters.Add("@duedate", dateTimePicker2.Value.ToString("yyyy-MM-dd"));
    sqlCommand.Parameters.Add("@subtotal", textBox6.Text);
    sqlCommand.Parameters.Add("@vat", textBox7.Text);
    sqlCommand.Parameters.Add("@total", textBox9.Text);
    sqlCommand.ExecuteNonQuery();

    sqlCommand.Parameters.Clear();
    sqlCommand.CommandText = "SELECT LAST_INSERT_ID();";
    var o = sqlCommand.ExecuteScalar();
    if (o is Int64)
    {
        var id = (Int64)o;
        foreach (DataGridViewRow row in mg2.Rows)
        {
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("@invno", id);
            sqlCommand.Parameters.AddWithValue("@pname", row.Cells[1].Value);
            sqlCommand.Parameters.AddWithValue("@qty", row.Cells[2].Value);
            sqlCommand.Parameters.AddWithValue("@unitprice", row.Cells[3].Value);
            sqlCommand.Parameters.AddWithValue("@amount", row.Cells[4].Value);
            sqlCommand.CommandText = InsertStatement2;
            sqlCommand.ExecuteNonQuery();
        }
    }
    sqlTrans.Commit();
}
Comment

PREVIOUS NEXT
Code Example
Sql :: run sql script file and changes db name in this file using c# 
Sql :: Work around for mutating problem in Oracle Triggers. Please check it out. 
Sql :: sql server isnull function nor working count 
Csharp :: how ot make a variable public without showing in the inspector 
Csharp :: messagebox yes-no 
Csharp :: unity string split 
Csharp :: unity scene load 
Csharp :: unity change tag in script 
Csharp :: c# app path 
Csharp :: c# get username 
Csharp :: how to check the tag of a collider in unity 
Csharp :: unity button onclick remove listener 
Csharp :: asp.net c# write string to text file 
Csharp :: create or update in laaravel 
Csharp :: how to detect collision in unity 
Csharp :: c# get date 
Csharp :: get max enum value c# 
Csharp :: c# char to int 
Csharp :: c# check if string is empty 
Csharp :: check last character of a string c# 
Csharp :: unity get speed of object 
Csharp :: c# convert string to double 
Csharp :: c# remove last value from list 
Csharp :: c# list object to json 
Csharp :: c# how to add newline on text box 
Csharp :: open new window c# wpf 
Csharp :: c# json to dictionary 
Csharp :: how to change rotate with script unity 
Csharp :: check if string is a guid c# 
Csharp :: c# socket receive 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =