UPDATE Ware
SET Price = Price * 1.02
WHERE WareNr > 0 AND WareNr <= 20000 AND Price >= 200
此代码在我的SQL服务器中运行良好。
现在我必须把它放到一个c#程序中,我不太清楚如何做到这一点。我对此非常陌生(获得了一些基本代码来进行更新),但我不确定如何做到这一点。
如果有人可以举例说明如何将其实现为“更新商品”按钮,我将非常感激
答案 0 :(得分:3)
有几种方法可以做到这一点。您可以使用SqlConnection
和SqlCommand
类;
using (SqlConnection connection = new SqlConnection(connectionString))
{
string queryString = "UPDATE Ware SET Price = Price * 1.02 WHERE WareNr > 0 AND WareNr <= 20000 AND Price >= 200";
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
的方式
using (SqlConnection connection = new SqlConnection(connectionString))
{
string queryString = "UPDATE Ware SET Price = @Price1 WHERE WareNr > @WareNr1 AND WareNr <= @WareNr2 AND Price >= @Price2";
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@Price1", 100 * 1.02);
command.Parameters.AddWithValue("@WareNr1", 0);
command.Parameters.AddWithValue("@WareNr2", 20000);
command.Parameters.AddWithValue("@Price2", 200);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
答案 1 :(得分:1)
string query = "UPDATE Ware SET Price = (Price * 1.02) WHERE WareNr > 0 AND WareNr <= 20000 AND Price >= 200";
using(SqlConnection conn = new SqlConnection("YourConnectionString"))
{
using(SqlCommand comm = new SqlCommand(query, conn))
{
conn.Open();
comm.ExecuteNonQuery();
}
}
答案 2 :(得分:1)
string connectionString = "Server=localhost;Database=dbName;Uid=Username;Pwd=Password";
using (var sqlConnection = new SqlConnection(connectionString))
{
var query = @"UPDATE Ware
SET Price = @Price
WHERE WareNr > 0 AND WareNr <= 20000 AND Price >= 200";
sqlConnection.Open();
using (var command = new SqlCommand(query, sqlConnection))
{
command.Parameters.AddWithValue("@Price", 100 * 1.02);
var dataReader = command.ExecuteNonQuery();
}
sqlConnection.Close();
}
答案 3 :(得分:0)
每个人都专注于如何进行更新的问题,但用户要求提供一个如何执行此操作的示例。
“给我举例说明如何将其实现为”更新商品“按钮”
这是我的贡献
的web.config
<configuration>
<connectionStrings>
<add name="connectionString" connectionString="Persist Security Info=True;User ID=YOUR_USER_ID;Password=YOUR_PASSWORD;Initial Catalog=YOUR_DATABASE_NAME;data source=LOCALHOST;Pooling=true;Connection Lifetime=60;Max Pool Size=100;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
ASPX
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
Price Rate:
<asp:TextBox ID="txtPriceRate" runat="server" />
<br />
New Price:
<asp:TextBox ID="txtNewPrice" runat="server" />
<br />
Limit Price:
<asp:TextBox ID="txtPriceLimit" runat="server" />
<br />
Min WareNr:
<asp:TextBox ID="txtWareMin" runat="server" />
<br />
Max WareNr:
<asp:TextBox ID="txtWareMax" runat="server" />
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
</asp:Content>
aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
// You will need to put some validations before try to convert the values
try
{
decimal priceRate = Convert.ToDecimal(this.txtPriceRate.Text);
decimal newPrice = Convert.ToDecimal(this.txtNewPrice.Text);
decimal priceLimit = Convert.ToDecimal(this.txtPriceLimit.Text);
int wareMin = Convert.ToInt32(this.txtWareMin.Text);
int wareMax = Convert.ToInt32(this.txtWareMax.Text);
Update(newPrice * priceRate, priceLimit, wareMin, wareMax);
}
catch
{
}
}
public static void Update(decimal paramPrice, decimal paramPriceLimit, int paramWareMin, int paramWareMax)
{
string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectionString"].ToString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
String query = "UPDATE Ware SET Price = @paramPrice WHERE WareNr > @paramWareMin AND WareNr <= @paramWareMax AND Price >= @paramPriceLimit";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.Add("@paramPrice", "paramPrice");
command.Parameters.Add("@paramPriceLimit", "paramPriceLimit");
command.Parameters.Add("@paramWareMin", "paramWareMin");
command.Parameters.Add("@paramWareMax", "paramWareMax");
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
}