VB.Net更新命令不工作
我是VB.Net新手,在使用Windows窗体应用程序时遇到了很多麻烦 我试图在表单上设置一个按钮来更新本地SQL Server数据库。下面是我当前保存按钮后面的代码。我已经调整了保存按钮后面的代码,现在看起来是这样的:VB.Net更新命令不工作,.net,vb.net,visual-studio-2012,.net,Vb.net,Visual Studio 2012,我是VB.Net新手,在使用Windows窗体应用程序时遇到了很多麻烦 我试图在表单上设置一个按钮来更新本地SQL Server数据库。下面是我当前保存按钮后面的代码。我已经调整了保存按钮后面的代码,现在看起来是这样的: Public Property InsertCommand As SqlCommand Public Property cn As SqlConnection Private Sub b_Save_Click(sender As Object,
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
当我运行此代码时,仍然会收到一条消息,说明数据已保存。但是,当我通过服务器资源管理器检查实际表时,没有添加任何内容。关于我做错了什么,您有什么建议吗?并不意味着更改(更改、删除或插入的行)已保存到数据库中。这只意味着此数据集的数据表
中至少有一行的行状态保持不变
您需要将DataAdapter
”Update
方法与相应的DeleteCommand
和UpdateCommand
s一起使用
因此,我假设您没有提供这些命令。您在表中添加了行了吗?然后您需要一个InsertCommand
。是否更改了数据行的字段
,然后需要更新命令
,是否调用了数据行。删除一行或多行上的
,然后需要提供删除命令
看一看这张照片
在代码示例中,您没有将新创建的DataRow
添加到DataTable
。因此,您需要使用自动生成的方法,其名称类似于:
DS_CV_Calls_DB.CV_Main_tbl.AddtblRow(row)
在调用DataAdapter.Update
之前,我最近遇到了同样的问题。如果在加载页面时设置任何输入值,则在“提交”页面时将再次设置这些相同的值。例如,这是我的页面加载事件的简化版本:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
displayValues()'this function sets all the input values on the page
End Sub
当我试图提交表单并运行update语句时,update语句正在运行,但没有更改任何值,因为displayValues()首先重置了输入值
事实证明,这是在回发时发生的,为了修复它,我添加了“IF Not IsPostBack”,以便仅在初始页面加载时自动加载值:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
displayValues()'this function sets all the input values on the page
End If
End Sub
希望这有帮助 对照代码中使用的连接字符串验证服务器资源管理器中使用的连接字符串。它们是一样的吗?仅供参考,DataSet、DataAdapter、TableAdapter都是旧技术。您应该尝试改用实体框架。简单多了。非常感谢您提供的信息。我真的很感激。恐怕我还是有点麻烦。我试着按照网站显示的方式设置代码,但它一直要求我输入连接字符串。我不知道如何得到它想要的。我尝试键入“数据源=(LocalDB)\v11.0;数据库=| DataDirectory |\CV_Calls_DB.mdf;Integrated Security=TRUE;”,但它告诉我字符串无法转换为SQL连接。我需要做什么?好的,我让它运行,但它仍然没有更新我的数据库。我需要做什么?