从sql数据库表获取varchar值到文本框 [英] Get a varchar value from a sql database table to a textbox

查看:100
本文介绍了从sql数据库表获取varchar值到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有窗户形式。







我想获得像1B这样的值从sql表名称作为productT的文本框。



这里是我的代码但它错了我认为

I have windows form.



I want to get a value like "1B" to a textbox from the sql table name as productT.

here is my code but it wrong i think

private void button5_Click(object sender, EventArgs e)
       {
           String commandstring;
           commandstring = "SELECT * FROM productT  WHERE spiceID='" + Convert.ToInt32(textBox1.Text) + "'";
           SqlDataReader dr1 = null;
           SqlCommand com2 = new SqlCommand(commandstring, conn1);
           try
           {
               conn1.Open();
           }
           catch
           {
               MessageBox.Show("error in seaching", "message", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
           }
           dr1 = com2.ExecuteReader();


           if (dr1.Read())
           {
               comboBox1.Text = dr1[1].ToString();
               dateTimePicker1.Value = (DateTime)dr1[2];
               dateTimePicker2.Value = (DateTime)dr1[3];
               textBox2.Text = dr1[4].ToString();
               textBox3.Text = dr1[5].ToString();
               textBox4.Text = dr1[].ToString();
           }
           else
           {
               MessageBox.Show("Record can not found");
           }
           dr1.Close();
           conn1.Close();
       }

推荐答案

这一行:

This line:
textBox4.Text = dr1[].ToString();



没有列索引。





编辑:

为什么要转换为Int32并将值用作字符串?


Don't have a column index.



Why do you convert to Int32 and use the value as a string?

commandstring = "SELECT * FROM productT  WHERE spiceID='" + Convert.ToInt32(textBox1.Text) + "'";



试试这个:


Try this:

commandstring = string.Format("SELECT * FROM productT  WHERE spiceID='{0}'", textBox1.Text.Trim());





刚刚在你的ccde上做了治疗,看到这个:





Just did a "heal" on your ccde, see this:

private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == string.Empty) return; 
           
            int id = 0;
            if (int.TryParse(textBox1.Text.Trim(), out id))
            {
                String commandstring;
                SqlDataReader dr1;
                SqlCommand com2;

                commandstring = string.Format("SELECT * FROM productT  WHERE spiceID='{0}'", id);

                com2 = new SqlCommand(commandstring, conn1);

                try
                {
                    conn1.Open();
                }
                catch
                {
                    MessageBox.Show("error in seaching", "message", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }

                try
                {
                    dr1 = com2.ExecuteReader();
                    if (dr1.Read())
                    {
                        comboBox1.Text = dr1[1].ToString();
                        dateTimePicker1.Value = (DateTime)dr1[2];
                        dateTimePicker2.Value = (DateTime)dr1[3];
                        textBox2.Text = dr1[4].ToString();
                        textBox3.Text = dr1[5].ToString();
                        textBox4.Text = dr1[].ToString();
                    }
                    else
                    {
                        MessageBox.Show("Record can not found");
                    }
                }
                catch
                {
                    if (!dr1.IsClosed) dr1.Close();
                    conn1.Close();
                } 
            }
        }


这篇关于从sql数据库表获取varchar值到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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