使用强类型数据集更新ASP.Net/VB.Net数据库
我们希望使用从中获取的值更新SQL Server 2012数据库中的数据 更改ASP.Net上的值使用强类型数据集更新ASP.Net/VB.Net数据库,asp.net,vb.net,dataset,strongly-typed-dataset,tableadapter,Asp.net,Vb.net,Dataset,Strongly Typed Dataset,Tableadapter,我们希望使用从中获取的值更新SQL Server 2012数据库中的数据 更改ASP.Net上的值DetailsView。我想使用 一个名为DataSetParentsDetails 名为parentsDetailStableApter 一个名为ParentsDetails的数据表 这些是使用数据集设计器创建的 这是代码隐藏文件中的代码,用于计算我们要更新到数据库中的数量: Protected Sub DetailsViewDetails_ItemCommand(sender As Obje
DetailsView
。我想使用
- 一个名为
DataSetParentsDetails
- 名为
parentsDetailStableApter
- 一个名为
的数据表ParentsDetails
Protected Sub DetailsViewDetails_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs)
Dim dcmAmountToAdjust As Decimal
Dim StrSqlStatement As String
Select Case e.CommandName
Case "Add"
Case "Edit"
dcmOriginalRegistrationFee = GetValueFromLabelRegistrationFee()
Case "Delete"
Case "Update"
dcmNewRegistrationFee = GetValueFromTextBoxRegistrationFee()
dcmAmountToAdjust = dcmNewRegistrationFee - dcmOriginalRegistrationFee
' Update the tuition balance in the parent's data.
'-------------------------------------------------
StrSqlStatement =
"Update Students " & _
"Set RegistrationCode = RegistrationCode + @AmountToAdjust " & _
"Where StudentID = @ID"
' Code to update the database goes here.
'---------------------------------------
End Select
End Sub
我确信这以前被问过很多次,但我找不到一个好的例子来说明如何使用中的查询:
StrSqlStatement
通过强类型数据集更新数据库。首先,您需要一个连接字符串,最好将连接字符串存储在web.config
文件中:
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=putYourServerAndInstanceNameHere;Initial Catalog=putYourDatabaseNameHere;User ID=putYourSqlUsernameHere;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
然后我们连接到数据库并运行命令,我们使用参数,因为它们更安全
'build the connection object using the string from the web.config file
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString)
'build the command object specifying the command text and the connection to use, conn
Using cmd As New SqlCommand("UPDATE Students SET RegistrationCode = RegistrationCode + @AmountToAdjust WHERE StudentID = @ID", conn)
'add the parameters needed by the command
cmd.Parameters.AddWithValue("@AmountToAdjust", amountToAdjust)
cmd.Parameters.AddWithValue("@ID", studentID)
'try to open the connection and execute the statement
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
'handle the exception here
End Try
End Using
End Using
请注意,这里不需要使用
conn.Close()
,因为Using
语句将为您解决这个问题(如果连接仍然打开,SqlConnection的Dispose方法将关闭连接)。首先,您需要一个连接字符串,最好将连接字符串存储在web.config
文件中:
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=putYourServerAndInstanceNameHere;Initial Catalog=putYourDatabaseNameHere;User ID=putYourSqlUsernameHere;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
然后我们连接到数据库并运行命令,我们使用参数,因为它们更安全
'build the connection object using the string from the web.config file
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString)
'build the command object specifying the command text and the connection to use, conn
Using cmd As New SqlCommand("UPDATE Students SET RegistrationCode = RegistrationCode + @AmountToAdjust WHERE StudentID = @ID", conn)
'add the parameters needed by the command
cmd.Parameters.AddWithValue("@AmountToAdjust", amountToAdjust)
cmd.Parameters.AddWithValue("@ID", studentID)
'try to open the connection and execute the statement
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
'handle the exception here
End Try
End Using
End Using
请注意,这里不需要使用
conn.Close()
,因为Using
语句将为您解决这一问题(如果连接仍然打开,SqlConnection的Dispose方法将关闭连接)。我是否遗漏了这里的要点,或者使用标准的SqlConnection和SqlCommand不会完成这项工作?您可以传入参数并指定数据类型、大小以及所需的一切。感谢您的回复。是的,如果这是更新数据库的最简单方法的话。请出示一个编码样本好吗?谢谢。我是没有抓住要点,还是使用标准的SqlConnection和SqlCommand不能完成这项工作?您可以传入参数并指定数据类型、大小以及所需的一切。感谢您的回复。是的,如果这是更新数据库的最简单方法的话。请出示一个编码样本好吗?谢谢。肖恩,你真是帮了大忙!非常感谢您的详细帮助。各位,请给这个家伙很多“这个答案很有用”的投票肖恩,你真是帮了大忙!非常感谢您的详细帮助。各位,请给这个家伙很多“这个答案很有用”的投票