asp.net中的更新....... [英] updation in asp.net.......

查看:93
本文介绍了asp.net中的更新.......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  Button4_Click(对象发​​件人,EventArgs e)
{
SqlConnection con = new SqlConnection(constrg);
cmd.Connection = con;
SqlDataReader dr;

con.Open();
cmd.CommandText = 从商店中选择countryname;
dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox1.Text = Convert.ToString(dr [ 0 ]);
con.Close();
if (TextBox1.Text == Convert.ToString(dr [ 0 ]))
{
con.Open();
cmd.CommandText = update store set currency =' + TextBox2.Text + < span class =code-string>
'where country =' + TextBox1.Text + ';

cmd.ExecuteNonQuery();
con.Close();
}
}



我需要进行更新,但在进行更新之前我需要检查我的数据库中是否存在countryname(textbox1)其他明智的不存在命令应该显示如果存在应该做更新

解决方案

兴奋地提出ryanb31的建议,你需要使用IF EXISTS,供参考,请参考下面的链接:< br $> b $ b

SQL:如果存在更新其他插入 [ ^ ]



如何检查SQL Server表中是否存在列 [ ^ ]



问候..:笑:


你可以尝试这个它可能对你有帮助。



 protected void Button4_Click(object sender,EventArgs e)
{
SqlConnection con = new的SqlConnection(constrg);
cmd.Connection = con;
SqlDataReader dr;
con.Open();
cmd.CommandText =从商店中选择国家名称;
dr = cmd.ExecuteReader();
while(dr.Read())
{
if(dr [0] .ToString()== TextBox1.Text)
{
cmd.CommandText = update store set currency ='+ TextBox2.Text +'where country ='+ TextBox1.Text +';
cmd.ExecuteNonQuery();
con.Close();
}
其他
{
//已经Exixt消息在这里
}
}

}
}


protected void Button4_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(constrg);
            cmd.Connection = con;
            SqlDataReader dr;
           
                con.Open();
                cmd.CommandText = "select countryname from store";
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    TextBox1.Text = Convert.ToString(dr[0]);
                    con.Close();
                    if (TextBox1.Text == Convert.ToString(dr[0]))
                    {
                        con.Open();
                        cmd.CommandText = "update store set currency='" + TextBox2.Text + "' where country='" + TextBox1.Text + "'";

                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }


I need to do updation,but before doing updation i need to do check that countryname(textbox1) is present in my database other wise not exist command should display if exist should do updation

解决方案

Excatly what suggested by ryanb31,you need to use IF EXISTS,for reference refer to below links:

SQL: If Exists Update Else Insert[^]

How to check if column exists in SQL Server table[^]

Regards..:laugh:


You can try This it might help you.

protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constrg);
cmd.Connection = con;
SqlDataReader dr;
con.Open();
cmd.CommandText = "select countryname from store";
dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr[0].ToString()==TextBox1.Text)
{
cmd.CommandText = "update store set currency='" + TextBox2.Text + "' where country='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
}
else
{
    // Already Exixt Message Here
}
}

    }
}


这篇关于asp.net中的更新.......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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