|
 |
|
สอบถามปํญหา INSERT IDENT_CURRENT ในรูปแบบ Parameter ต้องกำหนดอย่างไร |
|
 |
|
|
 |
 |
|
จากโค้ดดังกล่าวนี้
Code (SQL)
INSERT INTO MyTable
(Code, Name)
VALUES ('CODE' + REPLICATE('0', 8 - LEN(IDENT_CURRENT('MyTable'))) + CONVERT(nvarchar(50), IDENT_CURRENT('MyTable')), 'Thaicreate')
จะได้ผลลัพธ์แบบ

ผมต้องการเอาโค้ดดังกล่าวนี้ไป แทรก ใน โค้ดนี้อย่างไร
โค้ดที่ผมจะ Insert โดยประกาศตัวแปรและใช้ Parameters
ผมจะทำในรูปแบบใดบ้าง
รบกวนผู้รู้ หน่อยครับ
Code (C#)
string strConnectionString = "Data Source=PROGRAMMER03;Initial Catalog=DBTest;Integrated Security=SSPI;";
SqlConnection cn = new SqlConnection(strConnectionString);
cn.Open();
string strServiceType = comboServiceType.SelectedValue.ToString();
string strServiceArea = comboServiceArea.SelectedValue.ToString();
string strStatus = comboStatus.SelectedValue.ToString();
string query = "INSERT INTO tblServiceReq(ServiceReqID,ServiceType,ServiceArea,Status)VALUES(@ServiceReqID@strServiceType,@strServiceArea,@strStatus)";
SqlCommand InsertCommand = new SqlCommand(query, cn);
InsertCommand.Connection = cn;
//InsertCommand.Parameters.AddWithValue(@"strServiceReqID", strServiceReqID);
InsertCommand.Parameters.AddWithValue(@"strServiceType", strServiceType);
InsertCommand.Parameters.AddWithValue(@"strServiceArea", strServiceArea);
InsertCommand.Parameters.AddWithValue(@"strStatus", strStatus);
InsertCommand.ExecuteNonQuery();
MessageBox.Show("New tblServiceRequests Data has been added successfully");
cn.Close();
ซึ่งจะให้เพิ่มลงใน DB นี้ ที่มีฟิวชื่อ ServiceReqID

 
ขอบคุณล่วงหน้ามากครับ..
Tag : C#

|
|
 |
 |
 |
 |
Date :
2014-05-16 11:47:39 |
By :
ต้องการความรู้ |
View :
1060 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียน Query ปกติเลยครับ คุณจะใช้ Paramters ตัวที่ต้องการรับค่าเท่านั้นครับ ถ้าเป็นจาก Query ก็เขียนเหมือนเดิมครับ
|
 |
 |
 |
 |
Date :
2014-05-16 14:05:26 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูน่ะครับ
Code (SQL)
string query = "INSERT INTO tblServiceReq(ServiceReqID,ServiceType,ServiceArea,Status)VALUES('' + REPLICATE('0', 5 - LEN(IDENT_CURRENT('tblServiceReq'))) + CONVERT(nvarchar(50), IDENT_CURRENT('tblServiceReq')),@strServiceType,@strServiceArea,@strStatus)";

ปิดกระทุ้);
|
ประวัติการแก้ไข 2014-05-17 10:29:25
 |
 |
 |
 |
Date :
2014-05-16 21:14:31 |
By :
Zakariya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|