如何在此代码中添加验证(检查数据库中是否存在值) [英] How To Add validation( check if value exists in database) in this code

查看:81
本文介绍了如何在此代码中添加验证(检查数据库中是否存在值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SqlConnection sqlcon = new SqlConnection(@"Server=ADMIN-PC\SQLEXPRESS;Database=ELMANAR; Integrated Security = True");
        SqlDataAdapter da;
        DataTable dt1 = new DataTable();

        Data_Access_Layer.DataAccessLayer DAL = new Data_Access_Layer.DataAccessLayer();
        SqlCommand cmd = new SqlCommand();




        private void btnsave_Click(object sender, EventArgs e)
        {

            try
            {
              
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sqlcon;

                cmd.CommandText = "insert into TBL_CATEGORYCUSTOMERS (catcustomer_id,catcustomer_name,catcustomer_note) values ('" + txtid.Text + "','" + txtname.Text + "','" + txtnote.Text + "')";
                sqlcon.Open();
                cmd.ExecuteNonQuery();
              
                    
                    MessageBox.Show("Saved");
                    sqlcon.Close();
                    dt1.Clear();
                    da.Fill(dt1);

                    btnsave.Enabled = false;
                    btnadd.Enabled = true;
                    txtid.Clear();
                    txtname.Clear();
                    txtnote.Clear();
                                    
            }
       

            catch { }

推荐答案

要检查值是否存在,请使用Select语句。首先运行SELECT语句,检查返回值,然后根据需要继续。
To check if a value exists then you use a Select statement. Just run the SELECT statement first, check the return value, then proceed as you need to.


要检查值是否存在,请使用Select语句。只需先运行SELECT语句,检查返回值,然后根据需要继续。









谢谢你但是



我该怎么做
To check if a value exists then you use a Select statement. Just run the SELECT statement first, check the return value, then proceed as you need to.




thank you but

How Can I do that


这篇关于如何在此代码中添加验证(检查数据库中是否存在值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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