C# 如何使用asp.net在aspx.file中编辑Sql查询

C# 如何使用asp.net在aspx.file中编辑Sql查询,c#,asp.net,sql,C#,Asp.net,Sql,下面是我想在aspx文件中编辑的查询 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %> "InsertCommand="INSERT INTO [Sales] ([ReceiptID], [EmployeeID], [Discount], [Date]) VALUES

下面是我想在aspx文件中编辑的查询

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>
     "InsertCommand="INSERT INTO [Sales] ([ReceiptID], [EmployeeID], [Discount], [Date])
      VALUES (@ReceiptID, @EmployeeID, @Discount, @Date)">

我想用全局变量值替换@EmployeeID。

我该怎么做呢。这个全局变量保存在一个类中,我可以通过
global\u var\u session.GID
访问该类

我将从aspx中删除InsertCommand并将其移动到cs。 然后在相应事件处理程序下的.cs中:

SqlDatasource2.InsertCommand= "your query string with the reference to your Global Variable"

我将从aspx中删除InsertCommand并将其移动到cs。 然后在相应事件处理程序下的.cs中:

SqlDatasource2.InsertCommand= "your query string with the reference to your Global Variable"

您可以使用InsertParameters:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
 ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
 InsertCommand="INSERT INTO [Sales] ([ReceiptID], [EmployeeID], [Discount], [Date])
 VALUES (@ReceiptID, @Gid, @Discount, @Date)">

    <InsertParameters>
        <asp:Parameter Name="Gid" Type="String" />
        ...define other parameters...
    </InsertParameters>
</asp:SqlDataSource>

您可以使用InsertParameters:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
 ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
 InsertCommand="INSERT INTO [Sales] ([ReceiptID], [EmployeeID], [Discount], [Date])
 VALUES (@ReceiptID, @Gid, @Discount, @Date)">

    <InsertParameters>
        <asp:Parameter Name="Gid" Type="String" />
        ...define other parameters...
    </InsertParameters>
</asp:SqlDataSource>

你是说你的字段名实际上不是[EmployeeID],而是存储在global_var_session.GID中的值吗?很抱歉,看到编辑的问题,我想用全局值替换@EmployeeID。你是说你的字段名实际上不是[EmployeeID]但实际上是存储在global_var_session.GID中的值吗?很抱歉,看到编辑过的问题,我想用全局值替换@employeeid。