如何在C#ASP.NET的同一页面上使用插入和更新查询 [英] How to use both insert and update query on the same page in C# ASP.NET

查看:80
本文介绍了如何在C#ASP.NET的同一页面上使用插入和更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个订单摘要页面,其中正在从购物车表中填充字段。我在同一页面上添加了一个包含日期的新文本框。我希望用户在文本框中输入日期,然后所有信息都将保存在购物车表中。请让我知道如何在同一个c#页面上同时使用插入和更新查询



我尝试过:



 SqlConnection con = new SqlConnection(); 
con.ConnectionString =Data Source = .\\sqlexpress; Initial Catalog = college_education; User ID = sa; Password = system;;
con.Open();
string q =update cart set status ='CO'where user_name ='+ Session [user] +'and status ='ATC';
SqlCommand com = new SqlCommand(q,con);
com.ExecuteNonQuery();
{
Response.Write(< script> alert('您的订单已成功下单。我们的代理商会尽快与您联系。')< / script>);
}

解决方案

Op写道:

.i想在同一页面上同时使用update和insert语句。我只有一个按钮,该按钮将更新数据库中的某些字段并插入同一个表的数据库中的某些字段。是否有可能



是的,



 string update =update cart set status ='CO 'where user_name = @user and status ='ATC'; 
string insert =你的插入语句;
string query = update +;+ insert;
SqlCommand com = new SqlCommand(query,con);
com.Parameters.AddWithValue(@ user,Session [user]);
com.ExecuteNonQuery();


 SqlConnection con = new SqlConnection(); 
con.ConnectionString =Data Source = .\\sqlexpress; Initial Catalog = college_education; User ID = sa; Password = system;;
con.Open();
string q =update cart set status ='CO'where user_name ='+ Session [user] +'and status ='ATC';
SqlCommand com = new SqlCommand(q,con);
com.ExecuteNonQuery();
{
Response.Write(< script> alert('您的订单已成功下单。我们的代理商会尽快与您联系。')< / script>);
}

q =在此输入您的INSERT语句;
com = new SqlCommand(q,con);
com.ExecuteNonQuery();


i have a order summary page on which fields are being populated from the cart table.I I have added one new text box on the same page which contains the date. I want the user to enter the date in the text box and then all the information will be saved in the cart table. please let me know how to use both insert and update query on the same c# page

What I have tried:

SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=.\\sqlexpress;Initial Catalog=college_education;User ID=sa;Password=system;";
        con.Open();
        string q = "update cart set status='CO' Where user_name='" + Session["user"] + "' and status='ATC'";
        SqlCommand com = new SqlCommand(q,con);
        com.ExecuteNonQuery();
        {
             Response.Write("<script>alert('Your Order is Placed Successfully. Our Agent will contact you shortly.')</script>");
        }

解决方案

Op Wrote:

.i want to use both update and insert statement at the same time on same page. i have only one button and that button will update some fields in database and insert into some fields in database of the same table. is it possible


Yes,

string update = "update cart set status='CO' Where user_name= @user and status='ATC'";
           string insert = "Your insert statement";
           string query = update + " ; " + insert;
           SqlCommand com = new SqlCommand(query, con);
           com.Parameters.AddWithValue("@user", Session["user"]);
           com.ExecuteNonQuery();


SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=.\\sqlexpress;Initial Catalog=college_education;User ID=sa;Password=system;";
        con.Open();
        string q = "update cart set status='CO' Where user_name='" + Session["user"] + "' and status='ATC'";
        SqlCommand com = new SqlCommand(q,con);
        com.ExecuteNonQuery();
        {
             Response.Write("<script>alert('Your Order is Placed Successfully. Our Agent will contact you shortly.')</script>");
        }

        q = "PUT YOUR INSERT STATEMENT HERE;
        com = new SqlCommand(q,con);
        com.ExecuteNonQuery();


这篇关于如何在C#ASP.NET的同一页面上使用插入和更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆