如何验证组合框 [英] How do validate combobox

查看:64
本文介绍了如何验证组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的每一个身体!

在我的项目中,我正在加入工会理事会和村庄。现在我想验证它。我的意思是,如果我第一次加入viilage理事会。如果我添加它2或三次它添加到数据库。我想不添加它只添加一次。



我尝试过:



这是我的代码





公共部分类VillageCouncil:表格

{

public VillageCouncil()

{

InitializeComponent();

}

Dataaccess da = new Dataaccess();

// SqlDataReader reader;

private void VillageCouncil_Load(object sender,EventArgs e)

{

string q = string.Format(选择ID,来自UnionCouncil的UCouncilName);

DataTable dt = new DataTable();

SqlDataAdapter sd = new SqlDataAdapter(q,da.open());

sd.Fill(dt);

foreach(dt.Rows中的DataRow dr)

{

comboBox1.Items.Add(dr [1] .ToString());

}

//dt.Columns.Add(\"ID,typeof(string));

//dt.Columns.Add(\"UCouncilName,typeof(string));

//dt.Load(reader);



comboBox1.ValueMember =ID;

comboBox1.DisplayMember =UCouncilName ;

comboBox1.DataSource = dt;





}



private void button1_Click(object sender,EventArgs e)

{

string q = string.Format(insert into VillageCouncil(VCouncilName,UCName) values('{0}','{1}'),tbVCName.Text.Trim(),comboBox1.Text.Trim()); // .SelectedItem.ToString());

SqlCommand sc = new SqlCommand(q,da.open());

sc.ExecuteNonQuery();

}

}

}

HI every body!
in my project i am adding union council and Village. Now i want to validate it. I mean that if i add viilage council first time. and if i add it 2 or three time it has adding to database. i want to to not add this it only add one time.

What I have tried:

This is my Code


public partial class VillageCouncil : Form
{
public VillageCouncil()
{
InitializeComponent();
}
Dataaccess da = new Dataaccess();
//SqlDataReader reader;
private void VillageCouncil_Load(object sender, EventArgs e)
{
string q = string.Format("Select ID,UCouncilName from UnionCouncil");
DataTable dt = new DataTable();
SqlDataAdapter sd = new SqlDataAdapter(q, da.open());
sd.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr[1].ToString());
}
//dt.Columns.Add("ID", typeof(string));
//dt.Columns.Add("UCouncilName", typeof(string));
//dt.Load(reader);

comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "UCouncilName";
comboBox1.DataSource = dt;


}

private void button1_Click(object sender, EventArgs e)
{
string q = string.Format("insert into VillageCouncil(VCouncilName,UCName) values ('{0}','{1}')",tbVCName.Text.Trim(),comboBox1.Text.Trim());//.SelectedItem.ToString());
SqlCommand sc = new SqlCommand(q, da.open());
sc.ExecuteNonQuery();
}
}
}

推荐答案

0)不要使用格式化的字符串来形成查询。改为使用参数化查询。



1)我不明白为什么人们不编写存储过程而不是像这样在代码中手动干扰SQL。



2)正确编写的查询可以更新现有数据并插入新数据。例如,以下代码将尝试插入数据,如果未成功,则会插入数据。



0) DO NOT use formatted strings to form queries. Use parameterized queries instead.

1) I fail to understand why people don't write stored procedures instead of hand-jamming the SQL in code like this.

2) A properly written query can Update existing data AND Insert new data. For example, the following code will attempt to insert data, and if it did not succeed, it will insert it instead.

UPDATE database.dbo.mytable
    SET field1 = @param1
    WHERE field2 = @param2
IF @@ROWCOUNT = 0
    INSERT INTO database.dbo.mytable
           (field1, field2) VALUES (@param1, @param2)


这篇关于如何验证组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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