使用SqlCeDataReader C#填充文本框 [英] Fill Textbox with SqlCeDataReader C#

查看:94
本文介绍了使用SqlCeDataReader C#填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是: 

My code is : 

private void Modifier_Load(object sender, EventArgs e)
        {
            SqlCeConnection conn = new SqlCeConnection(@"Data Source =\Program Files\projetpfe\Inventaire.sdf");
            SqlCeConnection conne = new SqlCeConnection(@"Data Source =\Program Files\projetpfe\Inventaire.sdf");
            this.da = new SqlCeDataAdapter("Select Code_locale from Bureau", conn);
            this.da2 = new SqlCeDataAdapter("Select DISTINCT Adresse from Bureau ", conn);
            conn.Open();
            try
            {
                cmd = new SqlCeCommand("Select * from Bureau where Code_locale= 14852 ", conn);
                this.ds.Tables.Clear();
                this.da.Fill(this.ds, "Bureau");
                comboBox1.DataSource = this.ds.Tables["Bureau"];
                comboBox1.DisplayMember = "Code_locale";
                this.ds2.Tables.Clear();
                this.da2.Fill(this.ds2, "Bureau");
                comboBox2.DataSource = this.ds2.Tables["Bureau"];
                comboBox2.DisplayMember = "Adresse";
                conne.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {

                    textBox1.Text = dr.GetString(1).ToString();
                    textBox2.Text = dr.GetString(2).ToString();
                }
                conn.Close();
                conne.Close();
            }

这会返回一个好结果,所以这段代码工作正常,我从数据库和文本框中获取数据将使用SqlCeDataReader填充 

this return a good result , so this code works fine and i get data from database and textbox will got filled using SqlCeDataReader 

但是当我使用Combobox时,我没有得到任何东西 

But when i use Combobox , i didnt get anything 

这是不起作用的代码:

 private void Modifier_Load(object sender, EventArgs e)
        {
            SqlCeConnection conn = new SqlCeConnection(@"Data Source =\Program Files\projetpfe\Inventaire.sdf");
            SqlCeConnection conne = new SqlCeConnection(@"Data Source =\Program Files\projetpfe\Inventaire.sdf");
            this.da = new SqlCeDataAdapter("Select Code_locale from Bureau", conn);
            this.da2 = new SqlCeDataAdapter("Select DISTINCT Adresse from Bureau ", conn);
            conn.Open();
            try
            {
                cmd = new SqlCeCommand("Select * from Bureau where Code_locale= '"+comboBox1.GetItemText(comboBox1.SelectedItem)+"'", conn);
                this.ds.Tables.Clear();
                this.da.Fill(this.ds, "Bureau");
                comboBox1.DataSource = this.ds.Tables["Bureau"];
                comboBox1.DisplayMember = "Code_locale";
                this.ds2.Tables.Clear();
                this.da2.Fill(this.ds2, "Bureau");
                comboBox2.DataSource = this.ds2.Tables["Bureau"];
                comboBox2.DisplayMember = "Adresse";
                conne.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {

                    textBox1.Text = dr.GetString(1).ToString();
                    textBox2.Text = dr.GetString(2).ToString();
                }
                conn.Close();
                conne.Close();
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show("" + ex);
            }
      
        }

所以它返回此结果而没有文本框中的数据

So it return this result without data in textbox

所以我想当我选择一个组合框时,文本框中充满了datareader,我该怎么做?谢谢......

So i want to when i selected a combobox the textbox get filled with datareader , how can i do that? Thanks...

推荐答案

我建议使用如下所示的参数。我正在使用SqlClient数据提供程序,但对于SqlCe也是如此

I would recommend using parameters as shown below. I'm using SqlClient data provider but the same holds true for SqlCe

var cmd = new SqlCommand()
{
    CommandText = "Select * from Bureau where Code_locale=@Code_locale"
};
cmd.Parameters.AddWithValue("@Code_locale", comboBox1.Text)


现在,如果您重复使用该命令并运行另一个没有参数的查询,请先清除参数集。 br />

Now if you reuse the command and run another query without parameters first clear the parameter collection.


这篇关于使用SqlCeDataReader C#填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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