如何编写更新查询来使用 SQL 数据源更新两个表?
是否可以使用 SQL 数据源和 ASP.NET 网格视图更新两个表?我有以下用于 Select 语句的 SQL 查询。
SELECT
tbl_user_login.ID, tbl_user_login.UserID,
tbl_user_login.Pass, tbl_user_login.Enabled,
tbl_user_login.Permission, tbl_user_login.Rank,
tbl_user_profile.ID AS Expr1, tbl_user_profile.FName,
tbl_user_profile.LName, tbl_user_profile.Phone,
tbl_user_profile.Email1, tbl_user_profile.Email2
FROM
tbl_user_login
INNER JOIN
tbl_user_profile ON tbl_user_login.ID = tbl_user_profile.ID
但我不知道如何在 SQL 数据源
UPDATE
中编写更新和删除语句,所以我编写了存储过程。
CREATE PROCEDURE UpdateTwoTable
(
@ID int,
@UserID varchar(10),
@Pass varchar(50),
@Enabled int,
@Permission int,
@Rank int,
@FName varchar(50),
@LName varchar(50),
@Phone varchar(50),
@Email1 varchar(50),
@Email2 varchar(50)
) AS
BEGIN TRANSACTION
UPDATE tbl_user_login SET UserID = @UserID, Pass = @Pass, Enabled = @Enabled, Permission = @Permission, Rank = @Rank WHERE ID = @ID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
UPDATE tbl_user_profile SET FName = @FName, LName = @LName, Phone = @Phone, Email1 = @Email1, Email2 = @Email2 WHERE ID = @ID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
COMMIT
但我得到 Procedure or function UpdateTwoTable has too much argument returned.
UPDATE
我按照本指南操作,现在问题已解决。感谢所有提供帮助的人!
下面是我的 SQL 数据源。
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnString %>"
SelectCommand="SELECT tbl_user_login.ID, tbl_user_login.UserID, tbl_user_login.Pass, tbl_user_login.Enabled, tbl_user_login.Permission, tbl_user_login.Rank, tbl_user_profile.ID AS Expr1, tbl_user_profile.FName,
tbl_user_profile.LName, tbl_user_profile.Phone, tbl_user_profile.Email1, tbl_user_profile.Email2 FROM tbl_user_login INNER JOIN tbl_user_profile ON tbl_user_login.ID = tbl_user_profile.ID"
UpdateCommand="UpdateTwoTable" UpdateCommandType="StoredProcedure"
OldValuesParameterFormatString="Original_{0}">
<UpdateParameters>
<asp:Parameter Name="ID" />
<asp:Parameter Name="UserID"/>
<asp:Parameter Name="Pass"/>
<asp:Parameter Name="Enabled"/>
<asp:Parameter Name="Permission"/>
<asp:Parameter Name="Rank"/>
<asp:Parameter Name="FName"/>
<asp:Parameter Name="LName"/>
<asp:Parameter Name="Phone"/>
<asp:Parameter Name="Email1"/>
<asp:Parameter Name="Email2"/>
</UpdateParameters>
</asp:SqlDataSource>
Is it possible to update two tables using SQL Data Source and ASP.NET Grid View? I have the following SQL Query for the Select statement.
SELECT
tbl_user_login.ID, tbl_user_login.UserID,
tbl_user_login.Pass, tbl_user_login.Enabled,
tbl_user_login.Permission, tbl_user_login.Rank,
tbl_user_profile.ID AS Expr1, tbl_user_profile.FName,
tbl_user_profile.LName, tbl_user_profile.Phone,
tbl_user_profile.Email1, tbl_user_profile.Email2
FROM
tbl_user_login
INNER JOIN
tbl_user_profile ON tbl_user_login.ID = tbl_user_profile.ID
But I've no idea how do I write the update and delete statement in SQL Data Source
UPDATE
So I wrote the store procedure.
CREATE PROCEDURE UpdateTwoTable
(
@ID int,
@UserID varchar(10),
@Pass varchar(50),
@Enabled int,
@Permission int,
@Rank int,
@FName varchar(50),
@LName varchar(50),
@Phone varchar(50),
@Email1 varchar(50),
@Email2 varchar(50)
) AS
BEGIN TRANSACTION
UPDATE tbl_user_login SET UserID = @UserID, Pass = @Pass, Enabled = @Enabled, Permission = @Permission, Rank = @Rank WHERE ID = @ID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
UPDATE tbl_user_profile SET FName = @FName, LName = @LName, Phone = @Phone, Email1 = @Email1, Email2 = @Email2 WHERE ID = @ID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
COMMIT
But I get the Procedure or function UpdateTwoTable has too many arguments specified.
UPDATE
I followed this guide and now the problem is solved. Thanks to everyone who helped!
Below is my SQL Data Source.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnString %>"
SelectCommand="SELECT tbl_user_login.ID, tbl_user_login.UserID, tbl_user_login.Pass, tbl_user_login.Enabled, tbl_user_login.Permission, tbl_user_login.Rank, tbl_user_profile.ID AS Expr1, tbl_user_profile.FName,
tbl_user_profile.LName, tbl_user_profile.Phone, tbl_user_profile.Email1, tbl_user_profile.Email2 FROM tbl_user_login INNER JOIN tbl_user_profile ON tbl_user_login.ID = tbl_user_profile.ID"
UpdateCommand="UpdateTwoTable" UpdateCommandType="StoredProcedure"
OldValuesParameterFormatString="Original_{0}">
<UpdateParameters>
<asp:Parameter Name="ID" />
<asp:Parameter Name="UserID"/>
<asp:Parameter Name="Pass"/>
<asp:Parameter Name="Enabled"/>
<asp:Parameter Name="Permission"/>
<asp:Parameter Name="Rank"/>
<asp:Parameter Name="FName"/>
<asp:Parameter Name="LName"/>
<asp:Parameter Name="Phone"/>
<asp:Parameter Name="Email1"/>
<asp:Parameter Name="Email2"/>
</UpdateParameters>
</asp:SqlDataSource>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是可能的。以下是如何以安全的方式执行此操作:
这样,您就不必担心任何更新会引发一些错误。这意味着:如果其中任何一个失败,整个操作将被取消,并且数据库中不会有不一致的数据。
有关使用
Transactions
的更多信息,请参见此处:https://web.archive.org/web/20210513004758/ https://www.4guysfromrolla.com/webtech/080305-1.shtml基本的DELETE 语句 是:
DELETE FROM; WHERE <条件>
Yes, it's possible. Here is how to do it in a safe way:
In this way you don't need to be worried in case there is some error raised by any of the updates. Which means: If any of them will fail, the whole operation will be canceled and you will not have inconsistent data in your DB.
More about the use of
Transactions
here: https://web.archive.org/web/20210513004758/https://www.4guysfromrolla.com/webtech/080305-1.shtmlThe basic DELETE statement is:
DELETE FROM <tablename> WHERE <condition>
您可以将多个 sql 命令写入单个存储过程,如我上面所述。根据需要扩展更新。删除工作同样如此...
You can write multiple sql commands into a single sproc as I have above. Expand updates as needed. Deletes work the same way...
不起作用吗?
Doesn't it work?