如何比较表格中的数据和数据库中的数据? [英] how to compare data in Form and data from Database?

查看:194
本文介绍了如何比较表格中的数据和数据库中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

这是我的代码

Dear all,

here is my code

private void btnAdd_Click(object sender, EventArgs e)
       {
           SqlCommand com = new SqlCommand();
           com.Connection = cn;
           com.CommandType = CommandType.Text;

           //com.CommandText = "insert into Persons_info(perID, latinName, gender, dob, pob, phone, passport, curAdd, status) values(''" + txtID.Text + "'',''" + txtLatinName.Text + "'',''" + cbGender.Text + "''" + dTPdob.Text + txtPob.Text + "'',''" + txtPhone.Text + "'',''" + txtPassport.Text + "''" + txtCurAdd.Text + "''" + cbStatus.Text + " )";
           com.CommandText = "insert into tbl_stuInfo(perId, latinName, gender, dob, pob, phone, passport, curAdd, status, registerDate,majorName,acaYear) values (@perId, @latinName, @gender, @dob, @pob, @phone, @passport, @curAdd, @status, @registerDate, @majorName, @acaYear)";
           com.Parameters.Add(new SqlParameter("@perId", txtID.Text));
           com.Parameters.Add(new SqlParameter("@latinName", txtLatinName.Text));
           com.Parameters.Add(new SqlParameter("@gender", cbGender.Text));
           com.Parameters.Add(new SqlParameter("@dob", Convert.ToDateTime(this.dTPdob.Value).ToString("MM/dd/yyyy")));
           com.Parameters.Add(new SqlParameter("@pob", txtPob.Text));
           com.Parameters.Add(new SqlParameter("@phone", txtPhone.Text));
           com.Parameters.Add(new SqlParameter("@passport", txtPassport.Text));
           com.Parameters.Add(new SqlParameter("@curAdd", txtCurAdd.Text));
           com.Parameters.Add(new SqlParameter("@status", cbStatus.Text));
           com.Parameters.Add(new SqlParameter("@registerDate", Convert.ToDateTime(this.DTP_register.Value).ToString("MM/dd/yyyy")));
           com.Parameters.Add(new SqlParameter("@majorName", cbMajor.Text));
           com.Parameters.Add(new SqlParameter("@acaYear", txtAcayear.Text));
           com.ExecuteNonQuery();


           MessageBox.Show("Saving is done!");
       }


此代码用于将数据插入数据库(MS SQL Server).但是我不知道如何测试重复的号码.
perID是一个主键,因此它不能有重复的数字.所以我们必须测试这种情况.

请帮我.谢谢


This code is used for inserting data to database(Ms SQL server). But I don''t know how to test duplicate number.
perID is a primary key, so it cannot have the duplicate number. So we have to test this condition.

Please help me. Thanks

推荐答案

如果要插入新记录,为什么要让用户指定ID?
如果您想要的只是一个新的整数,则可以通过将IsIdentity设置为true来使其成为一个Identity字段,SQL会为您处理它.如果用户确实需要知道该号码,则在插入该行之后,将其显示为现有MessageBox的一部分.
否则,您必须告诉用户哪些是免费的",哪些可以在他们输入数据时轻松更改.
If you are inserting a new record, why are you letting the user specify the ID?
If all you want is a new integer, then make it an Identity field by setting IsIdentity to true, and SQL will handle it for you. If the user really needs to know the number, then show him after you have inserted the row, as part of your existing MessageBox.
Otherwise, you have to tell the user which ones are "free" and that could easily change while they are entering data.


我对为什么需要这样做感到困惑第一名.为什么此字段必须是主键?通常,通过创建Guid并将其用作密钥,或者通过从数据库获取密钥来自动为您生成唯一密钥.

考虑使用这种方法的情况.如果您告诉用户由于密钥不是唯一的,导致他们无法保存数据,那么您将为用户提供哪种类型的用户体验?他们是否必须一直尝试随机尝试字母的组合,直到碰到独特的组合?

也许您可以解释为什么需要这样做?
I''m confused as to why you need to do this in the first place. Why does this field have to be the primary key? Typically, unique keys are automatically generated for you, either by you creating a Guid and using that as the key, or by getting the key from the database.

Consider the case that you keep with this approach. What type of user experience are you going to be giving the user if you tell them that they cannot save the data because the key isn''t unique? Are they going to have to keep randomly trying combinations of letters until they hit on a unique combination?

Perhaps you could explain why you need to do this?


如果您将"perId"设置为数据库的主键,则无需从应用程序传递它.您只需要将isidentity设置为true,然后将该字段设置为数据库中的主键即可.

如果由于某种原因(我无法预料为什么)而无法在数据库中创建此主键,那么您可能希望先插入数据库一次,然后再插入为

If you have set "perId" as primary key is database then you need not pass it from the application. You just have to set the isidentity to true and set the field as primary key from the database.

If due to some reason(i couldn''t forsee why) you cannot make this primary key in database then you will perhaps want to dip into database once before inserting as

"select count(*) from tbl_stuInfo where perId = txtID.Text"



并仅在计数为0时插入,否则不插入.



and insert only if the count is 0 else not.


这篇关于如何比较表格中的数据和数据库中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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