所以我的意思是...假设我们有一个名为“ Matched_table”的表:
Date Game Client cost
12-12-2001 Game1 Jonh 200
12-12-2003 Game2 Jonathan 100
现在我们还有另一个名为“ NonMatched_table”的表,该表用于没有与“ costumer name”匹配的记录的记录,该表还允许用户更新Costumer name列以使记录成为“比赛记录”:
Date Game Costumer name cost
12-05-2001 Game1 Empty 200
12-05-2001 Game1 Empty 200
12-05-2001 Game1 Empty 200
12-05-2001 Game1 Carl 200 <--- Updated Record:
在更新时应发生:
----“匹配表” ----
Date Game Client cost
12-12-2001 Game1 Jonh 200
12-12-2003 Game2 Jonathan 100
12-05-2001 Game1 Carl 200
----“ NonMatched_table” ---
Date Game Costumer name cost
12-05-2001 Game1 Empty 200
12-05-2001 Game1 Empty 200
12-05-2001 Game1 Empty 200
不存在执行该操作的查询吗?我正在用C#执行此查询string query = "UPDATE NonMatched_table SET costumer= @costumer where ID = @Id Insert into Matched_table";
,我想知道我是否可以像之后的Insert into
或之前的样子,并且也可以删除记录以删除记录
答案 0 :(得分:1)
除非您有非常特殊的原因,否则可以采用更简单的方法:
MyTable
,其列为Date
,Game
,Client
,Cost
。只需使用SELECT
来过滤表。对于不匹配的记录,查询为:
SELECT * FROM MyTable WHERE Client IS NULL
对于匹配的记录,查询为:
SELECT * FROM MyTable WHERE Client IS NOT NULL
或者,您可以使用上述查询来创建MyTable
的两个视图,一个用于匹配记录,一个用于不匹配记录。
恐怕您的方法可能会迟早引起数据完整性问题。
更新
仅此而已,这是我建议的一个工作示例:
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource
ID="SqlDataSourceUnmatched"
runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Id], [Date], [Game], [Client], [Cost] FROM [MyTable] WHERE ([Client] IS NULL)"
UpdateCommand="UPDATE [MyTable] SET [Date]=@Date, [Game]=@Game, [Client]=@Client, [Cost]=@Cost WHERE [Id]=@Id"
OnUpdating="SqlDataSourceUnmatched_Updating">
<UpdateParameters>
<asp:Parameter Name="Id" Type="Int32"/>
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Game" />
<asp:Parameter Name="Client" />
<asp:Parameter Name="Cost" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:GridView
ID="GridViewUnmatched"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSourceUnmatched"
OnRowUpdated="GridViewUnmatched_RowUpdated">
<Columns>
<asp:BoundField
DataField="Id"
HeaderText="Id" />
<asp:BoundField
DataField="Date"
HeaderText="Date" />
<asp:BoundField
DataField="Game"
HeaderText="Game" />
<asp:BoundField
DataField="Client"
HeaderText="Customer Name" />
<asp:BoundField
DataField="Cost"
HeaderText="Cost" />
<asp:CommandField
ButtonType="Link"
ShowEditButton="true"
ItemStyle-Width="100" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSourceMatched"
runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Date], [Game], [Client], [Cost] FROM [MyTable] WHERE ([Client] IS NOT NULL)">
</asp:SqlDataSource>
<br />
<asp:GridView
ID="GridViewMatched"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSourceMatched">
<Columns>
<asp:BoundField
DataField="Date"
HeaderText="Date"/>
<asp:BoundField
DataField="Game"
HeaderText="Game" />
<asp:BoundField
DataField="Client"
HeaderText="Client" />
<asp:BoundField
DataField="Cost"
HeaderText="Cost" />
</Columns>
<EmptyDataTemplate>
No matched records.
</EmptyDataTemplate>
</asp:GridView>
</div>
</form>
</body>
</html>
背后的代码
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridViewUnmatched_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
GridViewMatched.DataBind();
}
protected void SqlDataSourceUnmatched_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
// assert the record update
e.Command.Parameters["@Date"].Value = DateTime.Now;
}
}
希望有帮助。我正在使用Visual Studio 2017,目标框架是.NET 4.6.1。