 |
|
ช่วยด้วยคร๊าบ!!! เพิ่มข้อมูลเข้า SQL แล้วติดตรง Mycommand.Parameters.Clear() |
|
 |
|
|
 |
 |
|
พอดีผมทำ Project จะเพิ่มข้อมูลเข้า Database แต่มันติด
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Public Class frmDevice
Private Mycommand As SqlCommand
Private dataSt As DataSet
Private adapter As SqlDataAdapter
Private bindingSrc As BindingSource
Private sql As String
Private Sub frmDevice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conStr As String = "Data Source=.\SQLEXPRESS;" &
"AttachDbFilename=|DataDirectory|\iConWorkDATA.mdf" &
"Integrated Security=True;User Instance=True"
Dim connection As SqlConnection
connection = New SqlConnection(conStr)
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnSaveAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAdd.Click
If IsDataComplete() = False Then
Exit Sub
End If
sql = "INSERT INTO Device"
sql = "(DeID, DeAsset, DeDv, Brand, Model, SnNo, WarY, EndWar, WarSub, Detail)"
sql = "VALUES(@devid, @devdv, @devbrand, @devmodel, @devsn, @devwary, @devendW, @devwars, @devdet"
AddParameters(sql)
Dim r As Integer = Mycommand.ExecuteNonQuery()
If r > 0 Then
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว")
btnSaveAdd.PerformClick()
Else
MessageBox.Show("เกิดข้อผิดพลาดในการบันทึกข้อมูล")
End If
End Sub
Private Sub AddParameters(ByVal sql As String)
Mycommand.Parameters.Clear() <<<<<<<<<<<<<<<<<<<<<<< ติดบรรทัดนี้
Mycommand.CommandText = sql
Mycommand.Parameters.AddWithValue("DeID", txtDevID.Text)
Mycommand.Parameters.AddWithValue("DeAsset", txtDevAsset.Text)
Mycommand.Parameters.AddWithValue("DeDv", txtDev.Text)
Mycommand.Parameters.AddWithValue("Brand", txtDevBrand.Text)
Mycommand.Parameters.AddWithValue("Model", txtDevModel.Text)
Mycommand.Parameters.AddWithValue("SnNo", txtDevSn.Text)
Mycommand.Parameters.AddWithValue("WarY", txtWarr.Text)
Dim dateStr As String = dtpDevWarDate.Value.Year
dateStr &= "/" & dtpDevWarDate.Value.Month
dateStr &= "/" & dtpDevWarDate.Value.Day
Mycommand.Parameters.AddWithValue("EndWar", dateStr)
Mycommand.Parameters.AddWithValue("WarSub", txtDevSub.Text)
Mycommand.Parameters.AddWithValue("Detail", txtDevDetail.Text)
End Sub
Private Function IsDataComplete() As Boolean
txtDevID.Text.Trim()
txtDev.Text.Trim()
txtDevBrand.Text.Trim()
txtDevModel.Text.Trim()
Dim errMsg As String = ""
If txtDevID.Text = "" Then
errMsg = "ยังไม่ได้ระบุรหัสเครื่อง"
ElseIf txtDev.Text = "" Then
errMsg = "ยังไม่ได้ระบุประเภทอุปกรณ์"
ElseIf txtDevBrand.Text = "" Then
errMsg = "ยังไม่ได้ระบุยี่ห้ออุปกรณ์"
ElseIf txtDevModel.Text = "" Then
errMsg = "ยังไม่ได้รุ่นของอุปกรณ์"
End If
If errMsg <> "" Then
MessageBox.Show(errMsg)
Return False
Else
Return True
End If
End Function
End Class
ไม่รู้ว่าผมใส่อะไรไม่ครบหรือรบกวนช่วยด้วยนะคร๊าบบบบบบบบบบบบ
Tag : .NET, Ms SQL Server 2008, VS 2010 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2013-03-11 17:34:32 |
By :
siraphat |
View :
897 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่มเติ่มครับใช้ Visual Basic 2010 ติดต่อฐานข้อมูล SQL Express 2008 แบบที่ติดตั้งมากับตัวโปรแกรม
|
 |
 |
 |
 |
Date :
2013-03-11 17:37:13 |
By :
siraphat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Error อะไรครับ ?
|
 |
 |
 |
 |
Date :
2013-03-12 09:42:01 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Private Mycommand As New SqlCommand
|
 |
 |
 |
 |
Date :
2013-03-12 10:08:59 |
By :
tee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|