本文介绍了在通过C#插入数据之前,如何检查表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void btnRegister_Click(object sender, EventArgs e)
        {
            string connetionString = "Data Source=qweq;Initial Catalog=Test_DB;User ID=asd;Password=asdsf";
            SqlConnection conn = new
            SqlConnection(connetionString);
            string commText = "insert into login(User_Name,Password,Address,City,State,PIN,Phone_no,Email_id,Created_By,Created_on) values('" + txtUserName.Text + "','" + txtPassword.Text + "','" + txtAddress.Text + "','" + txtCity.Text + "','" + txtState.Text + "','" + Convert.ToDouble(txtPin.Text) + "','" + Convert.ToDouble(txtPhoneNo.Text) + "','" + txtEmailId.Text +  "','" + "Alok" + "','" + DateTime.Now + "')";
            SqlCommand cmd = new 
            SqlCommand(commText, conn);
            conn.Open();
            int no = cmd.ExecuteNonQuery();
            conn.Close();
            if(no>0)
            {
                Label1.Text= "User register register sucessfully";
            }
            else
            {
                Label1.Text = "Error";
            }
            txtUserName.Text = String.Empty;
            txtPassword.Text = String.Empty;
            txtAddress.Text = String.Empty;
            txtCity.Text = String.Empty;
            txtState.Text = String.Empty;
            txtPin.Text = String.Empty;
            txtPhoneNo.Text = String.Empty;
            txtEmailId.Text = String.Empty;

        }







编辑:代码块添加




Edited: Code block Added

推荐答案

if not exists (select *from login where username=@username) 
begin
--Insert Query
end
else 
begin
--Update Query or something..
end



请参考

[]




这篇关于在通过C#插入数据之前,如何检查表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 19:20