|
|
|
ใน win form sqlcommand ตอน add parameter มันขึ้นขีดสีเขียวอะครับ แบบว่าสงสัยมาก |
|
|
|
|
|
|
|
ตัวโปรแกรมแนะนำให้ใช้ method AddWithValue ครับผม เนื่องจาก method Add มันเก่าแล้ว
|
|
|
|
|
Date :
2011-08-17 19:51:41 |
By :
anucham |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อ่อ ขอบคุณครับ
|
|
|
|
|
Date :
2011-08-17 20:05:34 |
By :
demon |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameters.Add or AddWithValue
Code (VB.NET)
Sub Sample1()
Dim objConn As System.Data.SqlClient.SqlConnection
Dim objCmd As System.Data.SqlClient.SqlCommand
Dim strConnString,strSQL As String
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New System.Data.SqlClient.SqlConnection(strConnString)
objConn.Open()
'*** FOR INSERT ***'
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
"VALUES (@sCustomerID,@sName,@sEmail,@sCountryCode,@sBudget,@sUsed)"
objCmd = New System.Data.SqlClient.SqlCommand(strSQL,objConn)
'*** Sample 1 ***'
'With objCmd
' .Parameters.AddWithValue("@sCustomerID","C005")
' .Parameters.AddWithValue("@sName","Weerachai Nukitram")
' .Parameters.AddWithValue("@sEmail","[email protected]")
' .Parameters.AddWithValue("@sCountryCode","TH")
' .Parameters.AddWithValue("@sBudget","2000000")
' .Parameters.AddWithValue("@sUsed","1000000")
'End With
'*** Sample 2 ***'
With objCmd
.Parameters.Add(New SqlParameter("@sCustomerID","C005"))
.Parameters.Add(New SqlParameter("@sName","Weerachai Nukitram"))
.Parameters.Add(New SqlParameter("@sEmail","[email protected]"))
.Parameters.Add(New SqlParameter("@sCountryCode","TH"))
.Parameters.Add(New SqlParameter("@sBudget","2000000"))
.Parameters.Add(New SqlParameter("@sUsed","1000000"))
End With
objCmd.ExecuteNonQuery()
lblText.Text = lblText.Text & "- Record Inserted"
'*** FOR UPDATE ***'
'strSQL = "UPDATE customer SET Budget = @sBudget " & _
'" WHERE CustomerID = @sCustomerID "
'objCmd = New System.Data.SqlClient.SqlCommand(strSQL,objConn)
'With objCmd
' .Parameters.Add(New SqlParameter("@sBudget","4000000"))
' .Parameters.Add(New SqlParameter("@sCustomerID","C005"))
'End With
'objCmd.ExecuteNonQuery()
'*** FOR DELETE ***'
'strSQL = "DELETE FROM customer " & _
'" WHERE CustomerID = @sCustomerID "
'objCmd = New System.Data.SqlClient.SqlCommand(strSQL,objConn)
'With objCmd
' .Parameters.Add(New SqlParameter("@sCustomerID","C005"))
'End With
'objCmd.ExecuteNonQuery()
objCmd = Nothing
objConn.Close()
objConn = Nothing
End Sub
Go to : ASP.NET System.Data.SqlClient - Parameter Query (SqlParameter)
|
|
|
|
|
Date :
2011-08-17 20:24:13 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
add ก็ใช้ได้ในบางกรณี
แต่ถ้าต้องการส่ง parameter แบบ .Add(string, object)
เขาแนะนำให้ใช้ .AddWithValue(string, object) แทน
เพราะ .Add(string, object) ตกรุ่นไปแล้วจ้า
|
|
|
|
|
Date :
2011-08-18 09:27:09 |
By :
พี่หมี |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|