如何将此代码转换为asp.net(Csharp) [英] how to convert this code into asp.net(Csharp)

查看:71
本文介绍了如何将此代码转换为asp.net(Csharp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void polulatetable_Click(object sender, EventArgs e)
        {
            

                SqlConnection con = new SqlConnection(con_str);
                con.Open();

                SqlCommand cmd = new SqlCommand("update t2 set t2.col1=t1.col1, t2.col2=t1.col2, from table1 as t1 join table2 as t2 on t1.cola =t2.cola and t1.colb=t2.colb  where  t1.colc =t2.colc and t2.colc   like '%2014-2015%'", con);
                int i = cmd.ExecuteNonQuery();
      
                con.Close();







i想要它编码而不是喜欢sql查询。

因为我必须提供条件



当cola colb colc匹配时

如果col1和col2为NULL然后

t2.col1 = t1.col3,t2.col2 = t1.col4



col3和cold4将更新..



任何人都可以帮我这个..




i want it into coding not like the sql query.
becos i have to put conditions

when cola colb colc is matching
if the col1 and col2 is NULL then
t2.col1=t1.col3, t2.col2=t1.col4

col3 and cold4 will update..

can anyone please help me with this..

推荐答案

你可以建立一个复杂 SqlCommand 执行任务或根据您的意图编写一些 C#代码。如果您喜欢后一种方法,那么请按小步骤进行编码,例如开始选择符合更新标准的记录。
You may either build a complex SqlCommand to perform the task or write, as per your intention, some C# code. If you like the latter approach then code it in small steps, for instance start selecting the records matching your 'update' criterion.


此代码完全正常工作......



感谢你们帮助我很多



protected void Button7_Click1(object sender,EventArgs e)

{



DataTable dt = new DataTable();

string query =select * from table1;

使用(SqlConnection con = new SqlConnection(con_str))

{

using(SqlCommand cmd = new SqlCommand(query))

{

使用(SqlDataAdapter da = new SqlDataAdapter())

{

cmd.CommandType = CommandType.Text;

cmd.Connection = con;

da.SelectCommand = cmd;

da.Fill(dt);

}

}

}



试试

{



string query2;

// string query3;

using(SqlConn ection =新的SqlConnection(con_str))

{

con.Open();



foreach( dt.Rows中的DataRow行)

{

string str = row [AdviceNo]。ToString();





//条件



if(string.IsNullOrEmpty(str))

{



query2 =update table2 set t2.alldetails_cheque = t1.chequeno from table1 as t1 and table2 as t2

on t1.id = t2.id和t1.name = t2.name其中t1.adviceno为null;

SqlCommand cmd2 = new SqlCommand(query2,con);

cmd2.ExecuteNonQuery( );

}

其他

{

query2 =update table2 set t2.alldetails_cheque = t1.adviceno从table1作为t1,table2作为t2

在t1.id = t2.id和t1.name = t2.name其中t1.chequeno为null;

SqlCommand cmd2 = new SqlCommand(query2,con);

cmd2.ExecuteNonQuery();





}



}



con.Close();

}



}

catch(异常) ex)

{

Response.Write(ex.Message);



}

终于

{



Label2.Text =updated;

}
This code is perfectly working...

Thank You Friends for helping me a lot

protected void Button7_Click1(object sender, EventArgs e)
{

DataTable dt = new DataTable();
string query = "select * from table1";
using (SqlConnection con = new SqlConnection(con_str))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
}
}
}

try
{

string query2;
// string query3;
using (SqlConnection con = new SqlConnection(con_str))
{
con.Open();

foreach (DataRow row in dt.Rows)
{
string str = row["AdviceNo"].ToString();


//The conditions

if (string.IsNullOrEmpty(str))
{

query2 = "update table2 set t2.alldetails_cheque=t1.chequeno from table1 as t1 and table2 as t2
on t1.id=t2.id and t1.name=t2.name where t1.adviceno is null";
SqlCommand cmd2 = new SqlCommand(query2, con);
cmd2.ExecuteNonQuery();
}
else
{
query2 = "update table2 set t2.alldetails_cheque=t1.adviceno from table1 as t1 and table2 as t2
on t1.id=t2.id and t1.name=t2.name where t1.chequeno is null ";
SqlCommand cmd2 = new SqlCommand(query2, con);
cmd2.ExecuteNonQuery();


}

}

con.Close();
}

}
catch (Exception ex)
{
Response.Write(ex.Message);

}
finally
{

Label2.Text = "updated";
}


这篇关于如何将此代码转换为asp.net(Csharp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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