C#ASP.net将数据更新到SQL服务器数据库:作业 [英] C# ASP.net Update Data into a SQL server database : Homework
问题描述
我的作业分配有问题。
我能够访问数据库并插入新客户但是当我尝试更新任何记录时它不起作用。
如果有人能告诉我我的代码有什么问题。
非常感谢
这是我的全部代码
< big> //这是我的问题//
Hi, I have a problem with my homework assignment.
I am able to access the database and insert new customer but it doesn''t work when i try to update any record.
if anyone can tell me what is wrong with my code.
thank you so much
this is all my code
//this is my problem//
protected void btnUpdate_Click(object sender, EventArgs e)
{
if (Request.QueryString["MODE"] != null)
{
//int CustomerID = Convert.ToInt32(Request.QueryString["id"]);
//Declare the connection object
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);
//Make the connection
Conn.Open();
//Define you query
// string updateSQL = "UPDATE [dbo].[xSample]" + " " + "Set Region = " + txtFirstName.Text + " " + "SalesID= 1000";
// string sql = "UPDATE [dbo].[Customer] SET [FirstName] = @FirstName WHERE CustomerID=@CustomerID";
string sql = "UPDATE [Customer] SET [FirstName] = @FirstName, [LastName] = @LastName, [Email] = @Email, [Password] = @Password, [Address1] = @Address1, [Address2] = @Address2, [City] = @City, [State] = @State, [zip] = @zip, [Phone] = @Phone, [Fax] = @Fax WHERE CustomerID=@CustomerID";
//Declare the Command
SqlCommand cmd = new SqlCommand(sql, Conn);
//Add the parameters needed for the SQL query
cmd.Parameters.AddWithValue("@CustomerID", Customer_ID());
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
cmd.Parameters.AddWithValue("@Email", txtEmailA.Text);
cmd.Parameters.AddWithValue("@Password", txtPasswordA.Text);
cmd.Parameters.AddWithValue("@Address1", txtAddress1.Text);
cmd.Parameters.AddWithValue("@Address2", txtAddress2.Text);
cmd.Parameters.AddWithValue("@City", txtCity.Text);
cmd.Parameters.AddWithValue("@State", txtState.Text);
cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
cmd.Parameters.AddWithValue("@Fax", txtFax.Text);
//Execute the query
cmd.ExecuteNonQuery();
Conn.Close();
Response.Redirect("Customers.aspx");
}
if (Request.QueryString["id"] == null)
{
//Declare the connection object
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);
//Make the connection
Conn.Open();
//Define you query
//string sql = "UPDATE [db_owner].[Customer] SET FirstName=@FirstName, LastName=@LastName, Email=@Email, Password=@Password, Address1=@Address1, Address2=@Address2, City=@City, State=@State, Zip=@Zip, Phone=@Phone, Fax=@Fax Where CustomerID=@CustomerID";
string sql = "INSERT INTO Customer(FirstName, LastName, Email, Password, Address1, Address2, City, State, Zip, Phone, Fax) VALUES(@FirstName, @LastName, @Email, @Password, @Address1, @Address2, @City, @State, @Zip, @Phone, @Fax)";
//string sqlN = "INSERT INTO [Customer] ([FirstName], [LastName], [Email], [Password], [Address1], [Address2], [City], [State], [Zip], [Phone], [Fax]) VALUES (@FirstName, @LastName, @Email, @Password, @Address1, @Address2, @City, @State, @Zip, @Phone, @Fax)";
//Declare the Command
SqlCommand cmd = new SqlCommand(sql, Conn);
//Add the parameters needed for the SQL query
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
cmd.Parameters.AddWithValue("@Email", txtEmailA.Text);
cmd.Parameters.AddWithValue("@Password", txtPasswordA.Text);
cmd.Parameters.AddWithValue("@Address1", txtAddress1.Text);
cmd.Parameters.AddWithValue("@Address2", txtAddress2.Text);
cmd.Parameters.AddWithValue("@City", txtCity.Text);
cmd.Parameters.AddWithValue("@State", txtState.Text);
cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
cmd.Parameters.AddWithValue("@Fax", txtFax.Text);
//Execute the query
cmd.ExecuteNonQuery();
Conn.Close();
Response.Redirect("Customers.aspx");
}
}
此代码中的问题不在更新中。任何人都可以告诉我它有什么问题
the problem in this code not in the update. anyone can tell me what is wrong with it
public int Customer_ID()
{
int CustomerID = 0;
CustomerID= int.Parse(Request.QueryString["id"]);
return CustomerID;
}
protected void Page_Load(object sender, EventArgs e)
{
this.Master.HighlightMenu = "Customers";
// if (Request.QueryString["MODE"] != null)
// {
if (Customer_ID()!=0)
{
txtFirstName.Text = "";
txtLastName.Text = "";
txtEmailA.Text = "";
txtEmailB.Text = "";
txtCity.Text = "";
txtPasswordA.Text = "";
txtPasswordB.Text = "";
txtFax.Text = "";
txtAddress1.Text = "";
txtAddress2.Text = "";
txtState.Text = "";
txtZip.Text = "";
txtPhone.Text = "";
//Declare the connection object
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);
//Make the connection
Conn.Open();
//Define you query
string sql = "SELECT * FROM [db_owner].[Customer] Where CustomerID=@CustomerID";
//Declare the Command
SqlCommand cmd = new SqlCommand(sql, Conn);
//Add the parameters needed for the SQL query
cmd.Parameters.AddWithValue("@CustomerID", Customer_ID());
//Declare the DataReader
SqlDataReader dr = null;
//Fill the DataReader
dr = cmd.ExecuteReader();
//Get the data
if (dr.Read() == false)
{
//No Records
dr.Close();
Conn.Close();
return;
}
txtFirstName.Text = dr["FirstName"].ToString();
txtLastName.Text = dr["LastName"].ToString();
txtEmailA.Text = dr["Email"].ToString();
txtEmailB.Text = dr["Email"].ToString();
txtPasswordA.Text = dr["Password"].ToString();
txtPasswordB.Text = dr["Password"].ToString();
txtAddress1.Text = dr["Address1"].ToString();
txtAddress2.Text = dr["Address2"].ToString();
txtCity.Text = dr["City"].ToString();
txtState.Text = dr["State"].ToString();
txtZip.Text = dr["Zip"].ToString();
txtFax.Text = dr["Fax"].ToString();
txtPhone.Text = dr["Phone"].ToString();
dr.Close();
Conn.Close();
}
}
推荐答案
我的问题是我忘了把回传放在页面上加载。
if(!IsPostBack)
{
my problem was i forgot to put postback in when the page load.
if (!IsPostBack)
{
cmd.Parameters.AddWithValue("@CustomerID", CustomerID);
您在哪里设置CustomerID?如果没有设置它将始终为0并且WHERE子句将不匹配任何记录。
您还应该学习使用语句
Where are you setting CustomerID? If it is not set it will always be 0 and the WHERE clause will not match any record.
You should also learn about using statements
using(SqlConnection conn = new SqlConnection(...))
{
}
这篇关于C#ASP.net将数据更新到SQL服务器数据库:作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!