【发布时间】:2013-12-17 21:59:28
【问题描述】:
我是 VB.Net 的新手,在使用 Windows 窗体应用程序时遇到了很多麻烦。
我的表单上有一个按钮,我试图将其设置为更新我的本地 SQL Server 数据库。这是我目前在“保存”按钮后面的代码。我调整了保存按钮后面的代码,现在看起来像这样:
Public Property InsertCommand As SqlCommand
Public Property cn As SqlConnection
Private Sub b_Save_Click(sender As Object, e As EventArgs) Handles b_Save.Click
Try
Dim row As CV_Calls_DBDataSet.CV_Main_tblRow = DS_CV_Calls_DB.CV_Main_tbl.NewCV_Main_tblRow
row.Call_Base_num = Me.CallNumber_mtx.Text
row.Cash_Vendor_Code = Me.cbo_Vendor.Text
row.Error_Code = Me.cbo_Error.Text
row.Error_Location_Code = Me.cbo_Location.Text
row.RowID = DS_CV_Calls_DB.CV_Main_tbl.Count + 1
DS_CV_Calls_DB.CV_Main_tbl.AddCV_Main_tblRow(row)
cn = New SqlConnection("Data Source=(LocalDB)\v11.0;Database=|DataDirectory|\CV_Calls_DB.mdf;Integrated Security=TRUE;")
CreateCallAdapter(cn)
'For Each row In DS_CV_Calls_DB.CV_Main_tbl.Rows
' Debug.WriteLine(row.RowState)
' Debug.WriteLine(row.Call_Base_num)
' Debug.WriteLine(row.Cash_Vendor_Code)
' Debug.WriteLine(row.Error_Location_Code)
' Debug.WriteLine(row.Error_Code)
' Debug.WriteLine(row.RowID)
'Next
TA_CV_Main.Update(DS_CV_Calls_DB.CV_Main_tbl)
CreateCallAdapter(cn)
MessageBox.Show(DS_CV_Calls_DB.HasChanges())
MessageBox.Show("Saved!")
Me.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Public Function CreateCallAdapter(ByVal connection As SqlConnection) As SqlDataAdapter
Dim adapter As SqlDataAdapter = New SqlDataAdapter
Dim command As SqlCommand = New SqlCommand("Select * FROM CV_Main_tbl", connection)
'Create InsertCommand
command = New SqlCommand("INSERT INTO CV_Main_tbl (Call_Base_num, Cash_Vendor_Code, Error_Location_Code, Error_Code) VALUES (@Call_Base_num, @Cash_Vendor_Code, @Error_Location_Code, @Error_Code)", connection)
'Add the parameters for the InsertCommand
command.Parameters.Add("@Call_Base_num", SqlDbType.Int, Me.CallNumber_mtx.Text)
command.Parameters.Add("@Cash_Vendor_Code", SqlDbType.SmallInt, Me.cbo_Vendor.Text)
command.Parameters.Add("@Error_Location_Code", SqlDbType.SmallInt, Me.cbo_Location.Text)
command.Parameters.Add("@Error_Code", SqlDbType.SmallInt, Me.cbo_Error.Text)
command.Parameters.Add("@RowID", SqlDbType.Int, DS_CV_Calls_DB.CV_Main_tbl.Count + 1)
adapter.InsertCommand = command
Return adapter
End Function
当我运行此代码时,我仍然收到一条消息,说数据已保存。但是,当我通过服务器资源管理器检查实际表时,没有添加任何内容。你对我做错了什么有什么建议吗?
【问题讨论】:
-
根据代码中使用的连接字符串验证服务器资源管理器中使用的连接字符串。它们是一样的吗?
-
仅供参考,DataSet、DataAdapter、TableAdapter 都是老技术。您应该尝试改用实体框架。这要简单得多。
标签: .net vb.net visual-studio-2012