如何使用C#中的一个组合框使用SQL数据库数据填充文本框 [英] How Do I Populate Text Boxes With Sql Database Data Using One Combo Box In C#

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

问题描述

嘿大家对SQL数据库函数都很陌生,但已经用c#编写了大约一年的时间,现在仍然不是那么好但是我到了那里!



我正在创建一个足球应用程序和编辑播放器和匹配我想使用一个下拉组合框从SQL数据库中检索数据然后会填充其他文本框和组合框。我自己去了,但不知道我哪里出错了所有的帮助都很感激



我的conn字符串是我的表单加载连接我到我的数据库然后使用这种方法将玩家ID数据放入组合框



Hey everyone pretty new to SQL Database functions but have been coding in c# for about a year now still not that great at it but I'm getting there!

I'm currently creating a football application and to Edit players and Matches i was wanting to use one drop down combo box to retrieve data from an SQL database which then would populate other text boxes and combo boxes. I've had a go at it myself but don't know where i'm going wrong all help is appreciated

My conn string is in my form load which connects me to my database i then use this method to put the Player ID data into the combo box

private void Navigate()
       {
           string showPlayers = "SELECT * From Add_Players";
           SqlCommand cmdData = new SqlCommand(showPlayers, conn);
           SqlDataReader myReader = cmdData.ExecuteReader();

           while (myReader.Read())
           {
               comboEditPlayer.Items.Add(myReader[0]);

           }


       }





之后我正在使用组合选择索引更改以尝试使用此代码填充其他框





After which i'm using the combo selectedindex change to try and populate the other boxs with this code

private void comboEditPlayer_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                conn.Open();
                string showPlayers = "SELECT * From Add_Players WHERE Player_ID ='" + comboEditPlayer + "'   ;";
                SqlCommand cmdData = new SqlCommand(showPlayers, conn);
                SqlDataReader myReader = cmdData.ExecuteReader();
                
            

                 while (myReader.Read())
                    {

                        comboEditPlayerPos.Items.Add(myReader[1]);
                        txtEditPlayerName.Text = myReader[2].ToString();
                        txtEditPlayerSecond.Text = myReader[3].ToString();
                        comboEditPlayerStatus.Items.Add(myReader[4]);
                        


                    }

                 conn.Close();
                 conn.Dispose();
            } 
            catch (Exception comboFail)
            {
                MessageBox.Show(comboFail.ToString());
            }
        }





再一次任何帮助表示我有很多工作要做这个就是绞乱我的大脑一旦我可以得到这个我可以多次使用它来帮助整个应用程序



谢谢



:)



once again any help appreciated i have a lot to do and this one is racking my brain once i can get this down i can use it multiple times to help the whole application

Thanks

:)

推荐答案

问题与 Player_ID ='+ comboEditPlayer +;

你直接将组合框设置为玩家ID。你需要设置所选项目值。

Player_ID ='+ comboEditPlayer.SelectedValue +'

因为你是使用''设置玩家ID值该列不应是数字类型列。如果它是数字类型列,则删除单引号。



您需要使用参数化查询来防止黑客在有价值的网站或其他数据库上进行尝试。

http://www.dotnetperls.com/sqlparameter [ ^ ]
problem with Player_ID ='" + comboEditPlayer + ";
you are directly set combobox as player id. you need to set selected item value.
Player_ID ='" + comboEditPlayer.SelectedValue+ "'
since you are set player id value with "' '" that column should not be a number type column. if it is number type column, remove the single quotes.

you need to use parameterized queries for preventing hacker attempts on valuable web sites or other databases.
http://www.dotnetperls.com/sqlparameter[^]


这篇关于如何使用C#中的一个组合框使用SQL数据库数据填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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