从C#/ASp.NET调用SQL过程
我一直在为一个雇主的网站上工作,该网站包含数据库信息,允许用户随意编辑信息。我收到的一个请求是添加一个名为Deactivate的按钮,onclick应该将标记“X”添加到名为“Inactive”的列中,说明该行实际上对任何选中复选框的行都是非活动的。复选框已添加到GridView中 我曾尝试通过SQL Server Management Studio添加存储过程,但是按照Microsoft文档中列出的教程,在右键单击存储过程文件夹时,我没有添加/创建新存储过程的选项 我已经在互联网上搜索了好几天,我不知道我是否没有找到正确的答案,或者我只是没有正确地理解它们 SQL停用命令伪代码从C#/ASp.NET调用SQL过程,c#,html,asp.net,sql-server,C#,Html,Asp.net,Sql Server,我一直在为一个雇主的网站上工作,该网站包含数据库信息,允许用户随意编辑信息。我收到的一个请求是添加一个名为Deactivate的按钮,onclick应该将标记“X”添加到名为“Inactive”的列中,说明该行实际上对任何选中复选框的行都是非活动的。复选框已添加到GridView中 我曾尝试通过SQL Server Management Studio添加存储过程,但是按照Microsoft文档中列出的教程,在右键单击存储过程文件夹时,我没有添加/创建新存储过程的选项 我已经在互联网上搜索了好几天
UPDATE Products SET Inactive="X" WHERE chkBox="True"
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Customer_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" InsertVisible="False" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" SortExpression="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" />
<asp:BoundField DataField="Inactive" HeaderText="Inactive" SortExpression="Inactive" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>"
DeleteCommand=
"DELETE FROM [Products] WHERE [ProductID] = @ProductID"
InsertCommand=
"INSERT INTO [Products] ([ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued], [Inactive]) VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued, @Inactive)"
SelectCommand=
"SELECT * FROM [Products]"
UpdateCommand=
"UPDATE [Products] SET [ProductName] = @ProductName, [SupplierID] = @SupplierID, [CategoryID] = @CategoryID, [QuantityPerUnit] = @QuantityPerUnit, [UnitPrice] = @UnitPrice, [UnitsInStock] = @UnitsInStock, [UnitsOnOrder] = @UnitsOnOrder, [ReorderLevel] = @ReorderLevel, [Discontinued] = @Discontinued, [Inactive] = @Inactive WHERE [ProductID] = @ProductID">
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="SupplierID" Type="Int32" />
<asp:Parameter Name="CategoryID" Type="Int32" />
<asp:Parameter Name="QuantityPerUnit" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Decimal" />
<asp:Parameter Name="UnitsInStock" Type="Int16" />
<asp:Parameter Name="UnitsOnOrder" Type="Int16" />
<asp:Parameter Name="ReorderLevel" Type="Int16" />
<asp:Parameter Name="Discontinued" Type="Boolean" />
<asp:Parameter Name="Inactive" Type="String" />
<asp:Parameter Name="ProductID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="SupplierID" Type="Int32" />
<asp:Parameter Name="CategoryID" Type="Int32" />
<asp:Parameter Name="QuantityPerUnit" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Decimal" />
<asp:Parameter Name="UnitsInStock" Type="Int16" />
<asp:Parameter Name="UnitsOnOrder" Type="Int16" />
<asp:Parameter Name="ReorderLevel" Type="Int16" />
<asp:Parameter Name="Discontinued" Type="Boolean" />
<asp:Parameter Name="Inactive" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:Button ID="ASPda" runat="Server" Text="Deactivate ASP" OnClick="Deactivate"/>
</form>
<p>
</p>
</body>
</html>
我想运行Deactivate命令的特定按钮
<asp:Button ID="ASPda" runat="Server" Text="Deactivate ASP" OnClick="Deactivate"/>
您被前端代码和SQL代码弄糊涂了 您的SQL伪代码应该类似于:
updateproducts SET Active=0,其中ProductID=@ProductID
上面的SQL查询将Active
列设置为0,其中ProductID
与您从应用程序传递的ID匹配(当然,只需将其调整为实际的列名和值)
然后在GridView
TemplateField
ItemTemplate
上添加以下标记:
然后在按钮上单击事件
foreach (GridViewRow rw in GridView.Rows) //Loop through all the rows in the GridView
{
if(rw.RowType == DataControlRowType.DataRow) //Checks if current row in the loop is a valid DataRow, not a Header or Footer row (which doesn't include the CheckBox and the data)
{
CheckBox cbProd = (CheckBox)rw.Cells[0].FindControl("chkRow"); //Finds the checkbox in the first column
HiddenField hfProd = (HiddenField)rw.Cells[0].FindControl("hfProdID"); //Finds the HiddenField in the first column where ProductID is stored
if(cbProd.Checked == true) //Checks if the current checkbox is selected, if yes, execute the UPDATE query by passing the Product ID as the parameter.
{
string sqlConnStr = ("put actual connection string here");
string sqlCmdStr = ("UPDATE Products SET Active = 0 WHERE ProductID = @ProductID"); //UPDATE query
using(SqlConnection sqlConn = new SqlConnection(sqlConnStr))
{
using(SqlCommand sqlCmd = new SqlCommand(sqlCmdStr, sqlConn))
{
sqlConn.Open();
sqlCmd.Parameters.Clear();
sqlCmd.Parameters.AddWithValue("ProductID", hfProd.Value); //Product ID saved in HiddenField
sqlCmd.ExecuteNonQuery();
sqlConn.Close();
}
}
}
}
}
试试这个:
public static DataTable GetData()
{
String callProcedure = "exec my_proc";
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("connection string here!"))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = callProcedure;
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
adp.Fill(dt);
return dt;
}
}
}
}
你什么意思?你想知道如何创建存储过程吗?好吧,我只是想在我的网站上请求帮助运行SQL代码,但是如果我确实需要通过创建存储过程来解决这个问题,那么任何帮助都将不胜感激。正如你所知,我对C#和SQL相当陌生,因此如果我感到沮丧,我向你道歉。在你说的点击按钮事件中,我该如何准确地添加它。我是在C#中添加程序/函数还是将其添加到ASP.NET?从设计视图中,您需要双击按钮,它会将您重定向到代码隐藏,将创建一个
ASPda_单击
事件。您需要复制粘贴并根据需要调整我提供的代码。我根据需要编辑了您的代码后运行了它。现在我得到了很多错误的陈述;预期和}预期。另外,您给我的HiddenField标记返回以下错误:System.Web.UI.WebControls.DataControlFieldCollection必须具有“System.Web.UI.WebControls.DataControlField”类型的项asp:HiddenField的类型是System.Web.UI.WebControl.HiddenField。哦,糟糕,我还以为是vb.net。我将它转换为c#。您声明我必须插入存储过程的名称,但是,在我提出的问题中,由于某些原因,我无法创建存储过程,SQL Server Management Studio中没有这样做的选项。当我右键单击对象资源管理器中的存储过程文件夹时,菜单上没有任何选项。我应该添加什么来代替基于此的连接字符串:ConnectionString=“”
?那是app.config中的连接字符串吗?我没有任何app.config文件,但是那里的连接字符串是由Visual Studio自动生成的,到目前为止,它一直在连接到数据库。它直接取自ASP.NET代码尝试将连接分配给字符串str='yourconnection string here',然后调试并检查str是否是连接的正确字符串值。然后将其放置在新的SqlConnection(str)中。