 |
|
การ save ข้อมูลลง database การไช้คำสั่ง sql save ต่างตาราง |
|
 |
|
|
 |
 |
|
สั้นๆน้อยๆดีเน้นกระทัดรัดเรอะครับ
บ้านๆครับ
SQL Insert/Update
ร่วมกับ for
ง่ายๆสั้นๆกระทัดรัด  
|
 |
 |
 |
 |
Date :
2015-09-21 15:29:00 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โค๊ด จะประมานไหนครับ
|
 |
 |
 |
 |
Date :
2015-09-21 15:36:02 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ประชดครับ(โอ๊ยน๊   )
มีข้อมูล datable sql หรือโค๊ดที่ทำไปแล้วมั่งครับ
|
 |
 |
 |
 |
Date :
2015-09-21 16:03:08 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่มีครับ งง อยู่ ว่าต้อง ไช้ยังไง SQL Insert/Update กับ inner join อ่าครับ เดี๋ยว ผม ลองดูก่อน นะครับ ขอบคุณค๊าบบ
ตอนนี้ได้แค่นี้ แต่ ติดตรง ที่ ใน datagridview อ่าครับ ที่บอก เอาบาง cell แต่หลาย rows แล้วก็ตรง combobox อ่าครับ มันไช้ยังไง งงครับ
เดี๋ยว ผม ลองดูก่อน นะครับ ขอบคุณค๊าบบ
Code (C#)
sda = new SqlDataAdapter("INSERT INTO receives values('" + txt_receive_no.Text + "','" + txt_invoice_no.Text + "')", conn);
|
 |
 |
 |
 |
Date :
2015-09-21 16:16:26 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อยากรู้ว่า objCmd มันคือ ตัว อะไรอ่าครับ
ได้มาจาก https://www.thaicreate.com/dotnet/forum/065762.html
Code (C#)
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
strsql = "insert into OrderDetail(OrderID,ProductID,NumberOrder,UnitID,Weight,UnitWeightID,Driver,RegisDriver) values(@OrderId,@ProductID,@ProductName,@NumberOrder,@UnitID,@Weight,@UnitWeightID,@Driver,@RegisDriver)"
objCmd = New System.Data.SqlClient.SqlCommand(strsql, DbConn)
For i As Integer = 0 To DataGridOrder.RowCount - 1
If Not DataGridOrder.Rows(i).IsNewRow Then
With objCmd
.Parameters.Add(New SqlParameter("@OrderId", orderID.Text))
.Parameters.Add(New SqlParameter("@ProductID", DataGridOrder.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@NumberOrder", DataGridOrder.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@UnitId", DataGridOrder.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@Weight", DataGridOrder.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@UnitWeightID", DataGridOrder.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@Driver", Driver.Text))
.Parameters.Add(New SqlParameter("@RegisDriver", RegisDriver.Text))
End With
objCmd.ExecuteNonQuery()
objCmd.Parameters.Clear()
End If
Next
MessageBox.Show("บันทึกข้อมูลแล้ว")
DbConn.Close()
End Sub
|
 |
 |
 |
 |
Date :
2015-09-21 16:55:26 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มัน eror ว่า Incorrect syntax near '@product_code'. ครับ ที่บรรทัด scm2.ExecuteNonQuery(); อ่าครับ ต้องทำอย่างไรดีครับ
Code (C#)
private void btnsave_receive_CheckedChanged(object sender, EventArgs e)
{
SqlCommand scm2 = new SqlCommand("receive_details (product_code , product_serial, product_name, product_unit_name, product_unit_num, remark, receive_status_id) values (@product_code , @product_serial, @product_name, @product_unit_name, @product_unit_num,@remark, @receive_status_id)", conn);
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (!dataGridView1.Rows[i].IsNewRow)
{
scm2.Parameters.AddWithValue(" @product_code ", dataGridView1.Rows[i].Cells[2].Value);
scm2.Parameters.AddWithValue(" @product_serial ", dataGridView1.Rows[i].Cells[3].Value);
scm2.Parameters.AddWithValue(" @product_name ", dataGridView1.Rows[i].Cells[4].Value);
scm2.Parameters.AddWithValue(" @product_unit_name ", dataGridView1.Rows[i].Cells[5].Value);
scm2.Parameters.AddWithValue(" @remark ", dataGridView1.Rows[i].Cells[7].Value);
scm2.Parameters.AddWithValue(" @receive_status_id ", dataGridView1.Rows[i].Cells[9].Value);
scm2.Parameters.AddWithValue(" @product_unit_num ", dataGridView1.Rows[i].Cells[10].Value);
}
}
conn.Open();
scm2.ExecuteNonQuery();
conn.Close();
DialogResult dialogResult = MessageBox.Show("Save success", "Result", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
load_default();
textboxNoEnable();
}
else if (dialogResult == DialogResult.No)
{
//เลือก No
}
|
 |
 |
 |
 |
Date :
2015-09-22 12:07:57 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
INSERT INTO หายไปไหนแล้วละ
|
 |
 |
 |
 |
Date :
2015-09-22 12:12:10 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่ม เข้าไปแล้วครับ ก็ error แบบ เดิม ครับ
|
 |
 |
 |
 |
Date :
2015-09-22 13:02:53 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
scm2.Parameters.AddWithValue(" @product_code ", dataGridView1.Rows[i].Cells[2].Value);
scm2.Parameters.AddWithValue(" @product_serial ", dataGridView1.Rows[i].Cells[3].Value);
scm2.Parameters.AddWithValue(" @product_name ", dataGridView1.Rows[i].Cells[4].Value);
scm2.Parameters.AddWithValue(" @product_unit_name ", dataGridView1.Rows[i].Cells[5].Value);
scm2.Parameters.AddWithValue(" @remark ", dataGridView1.Rows[i].Cells[7].Value);
scm2.Parameters.AddWithValue(" @receive_status_id ", dataGridView1.Rows[i].Cells[9].Value);
scm2.Parameters.AddWithValue(" @product_unit_num ", dataGridView1.Rows[i].Cells[10].Value);
พวกนี้ครับ ลองตัดช่องว่างออกให้หมดครับ ตรง " @product_code " จะเป็น "@product_code"
|
 |
 |
 |
 |
Date :
2015-09-22 13:29:35 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครั้งนี้ error นี้ครับ The variable name '@product_code' has already been declared. Variable names must be unique within a query batch or stored procedure. ที่บรรทัดเดิมครับ scm2.ExecuteNonQuery();
|
 |
 |
 |
 |
Date :
2015-09-22 13:41:07 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอดู โค้ดล่าสุดหน่อยครับ
|
 |
 |
 |
 |
Date :
2015-09-22 14:01:47 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี่ครับ
Code (C#)
SqlCommand scm2 = new SqlCommand("INSERT INTO receive_details (product_code , product_serial, product_name, product_unit_name, product_unit_num, remark, receive_status_id) values (@product_code, @product_serial, @product_name, @product_unit_name, @product_unit_num,@remark, @receive_status_id) ", conn);
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (!dataGridView1.Rows[i].IsNewRow)
{
scm2.Parameters.AddWithValue("@product_code", dataGridView1.Rows[i].Cells[2].Value);
scm2.Parameters.AddWithValue("@product_serial", dataGridView1.Rows[i].Cells[3].Value);
scm2.Parameters.AddWithValue("@product_name", dataGridView1.Rows[i].Cells[4].Value);
scm2.Parameters.AddWithValue("@product_unit_name", dataGridView1.Rows[i].Cells[5].Value);
scm2.Parameters.AddWithValue("@remark", dataGridView1.Rows[i].Cells[7].Value);
scm2.Parameters.AddWithValue("@receive_status_id", dataGridView1.Rows[i].Cells[9].Value);
scm2.Parameters.AddWithValue("@product_unit_num", dataGridView1.Rows[i].Cells[10].Value);
}
}
conn.Open();
scm2.ExecuteNonQuery();
conn.Close();
|
 |
 |
 |
 |
Date :
2015-09-22 14:03:45 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่มันขึ้นแบบนี้เพราะมันไม่มีข้อมูลครับ หรือ มันไม่เข้า For
|
 |
 |
 |
 |
Date :
2015-09-22 14:08:42 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อมูลมีนะครับ อันนี้ผมลองดีบัคดู

error 1

error 2

|
 |
 |
 |
 |
Date :
2015-09-22 14:20:34 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ย้ายตัวนี้ scm2.ExecuteNonQuery(); เข้าไปไว้ใน If ครับ แล้ว conn.Open(); เอาไว้บน For
|
 |
 |
 |
 |
Date :
2015-09-22 14:25:41 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลืมบอกไปอย่าง
มันไม่ได้เริ่มต้น Cells จาก 1 แต่มันเริ่ม จาก 0 ครับ
อย่าลืมแก้ด้วยครับ
 
|
 |
 |
 |
 |
Date :
2015-09-22 14:31:40 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือ มัน เออเร่อแบบเดิมครับ บางครั้งก็เซฟ ได้ บางครั้ง เลือก 2 ตัว แล้ว เซฟ มันเข้า ไปเซฟ ตัวเดียว ส่วนอีกตัวขึ้น error ครับ

|
 |
 |
 |
 |
Date :
2015-09-22 14:46:20 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (!dataGridView1.Rows[i].IsNewRow)
{
SqlCommand scm2 = new SqlCommand("INSERT INTO receive_details (product_code , product_serial, product_name, product_unit_name, product_unit_num, remark, receive_status_id) values (@product_code, @product_serial, @product_name, @product_unit_name, @product_unit_num,@remark, @receive_status_id) ", conn);
if (conn.State == ConnectionState.Closed)
conn.Open();
scm2.Parameters.AddWithValue("@product_code", dataGridView1.Rows[i].Cells[2].Value);
scm2.Parameters.AddWithValue("@product_serial", dataGridView1.Rows[i].Cells[3].Value);
scm2.Parameters.AddWithValue("@product_name", dataGridView1.Rows[i].Cells[4].Value);
scm2.Parameters.AddWithValue("@product_unit_name", dataGridView1.Rows[i].Cells[5].Value);
scm2.Parameters.AddWithValue("@remark", dataGridView1.Rows[i].Cells[7].Value);
scm2.Parameters.AddWithValue("@receive_status_id", dataGridView1.Rows[i].Cells[9].Value);
scm2.Parameters.AddWithValue("@product_unit_num", dataGridView1.Rows[i].Cells[10].Value);
scm2.ExecuteNonQuery();
}
}
conn.Close();
ตามนี้เลย
|
 |
 |
 |
 |
Date :
2015-09-22 15:01:08 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้ แล้วครับ ขอบคุณ ทั้งสองท่านมากครับ ช่วย อทิบายหน่อย ได้มั้ยครับ ตรง if (conn.State == ConnectionState.Closed) อ่าครับ
ขอบคุณครับ Y^Y
|
 |
 |
 |
 |
Date :
2015-09-22 15:13:25 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
   
เป็นมหากาพย์เลยนะครับ
แต่ก็ดีที่ทำได้ครับ
|
 |
 |
 |
 |
Date :
2015-09-22 15:21:01 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
if (conn.State == ConnectionState.Closed)
conn.Open();
ตรวจสอบสถานะการเชื่อมต่อกับฐานข้อมูล ถ้าการเชื่อมต่อปิดอยู่ ให้เปิดการเชื่อมต่อใหม่
|
 |
 |
 |
 |
Date :
2015-09-22 15:26:30 |
By :
Freedom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากๆ ครับ
|
 |
 |
 |
 |
Date :
2015-09-22 15:36:18 |
By :
phuriwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ส่งคำขอบคุณแทนแล้วนะ ส่วนน้ำสีเหลืองมีฟองผมขอแล้วกัน 5555(ไม่เอาที่ปล่อยจากตัวนะ 555)
|
ประวัติการแก้ไข 2015-09-22 15:41:33
 |
 |
 |
 |
Date :
2015-09-22 15:40:50 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จาดปายครับ
พระราม 3 ซอย 23 ครับ
  
|
 |
 |
 |
 |
Date :
2015-09-22 15:59:34 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|