简体   繁体   English

将数据库数据插入列表框C#

[英]Insert database data into listbox C#

Disclaimer: I have no prior experience with querying databases from c# code (so go easy on me) 免责声明:我以前没有从c#代码查询数据库的经验(所以对我来说很容易)

I am trying to insert data from my SQL Server database into my listbox. 我试图将我的SQL Server数据库中的数据插入到我的列表框中。 Right now I am trying this in the form of an array. 现在我以数组的形式尝试这个。 I first connect to the database, and then insert the "state" from the database into the index of the array. 我首先连接到数据库,然后将数据库中的“状态”插入到数组的索引中。 I want all 50 states to be put into my array and then this information to be put into my listbox. 我希望将所有50个状态放入我的数组中,然后将此信息放入我的列表框中。 Right now, my data is being inserted but when I view it in the list box it shows System.Data.SqlClient.SqlCommand . 现在,我的数据正在插入,但当我在列表框中查看它时,它显示了System.Data.SqlClient.SqlCommand

public string connString = "Not displaying this for security reasons, it is set up correctly though."; //Setting up the connection to my DB

public frmState()
{
        InitializeComponent();

        this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.frmState_FormClosed);

        using (SqlConnection dbConn = new SqlConnection(connString))
        {
            dbConn.Open();
            string cmdString = "select State_Name from [State]";

            SqlCommand cmd = new SqlCommand(cmdString, dbConn);

            SqlDataReader reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                        string[] stateList = new string[50];

                        for (int i = 1; i <= 50; i++)
                        {
                            stateList[i - 1] = cmd.ToString();
                        }

                        for (int i = 0; i < stateList.Length; i++)
                        {
                            lbStates.Items.Add(stateList[i].ToString());
                        }
                }
            }
            finally
            {
                reader.Close();
            }
        }
    }

Also, I am aware that as of right now I will be showing the same state 50 times. 此外,我知道现在我将展示相同的状态50次。 I am trying to figure out how to insert one state at a time. 我试图弄清楚如何一次插入一个状态。 Is this an efficient way of doing this? 这是一种有效的方法吗? Also, any tips on working with databases in c#? 另外,有关在c#中使用数据库的任何提示吗? I am on Visual Studio 2017 and Microsoft SQL Server 2016. 我在Visual Studio 2017和Microsoft SQL Server 2016上。

The problem comes from where you did: 问题来自你所做的:

stateList[i - 1] = cmd.ToString();

It's wrong because you are converting an SqlCommand object to string and putting it inside an array of type of string to retrieve data from your SqlCommand. 这是错误的,因为您正在将SqlCommand对象转换为字符串并将其放入字符串类型的数组中以从SqlCommand中检索数据。

Changing the above line as below will fix your problem: 如下更改以上行将解决您的问题:

tateList[i - 1] = reader.GetString(0);

any tips on working with databases in c#? 有关在c#中使用数据库的任何提示?

for a beginner with C# and SQL, I suggest you to keep learning basic database access tools of ADO.net like using SqlDataReader, SqlDataReader, SqlDataAdapter, ... . 对于初学者使用C#和SQL,我建议你继续学习ADO.net的基本数据库访问工具,比如使用SqlDataReader,SqlDataReader,SqlDataAdapter,.... but to have professional and of course secure application witch also needs to be simple; 但要拥有专业,当然还有安全应用,女巫也需要简单; you have to move toward using ORM tool (witch are medium to access database securely) like "Entity Framework", linq, ... witch will make talking to database much more convenient. 你必须转向使用ORM工具(安全地访问数据库),例如“实体框架”,linq,...女巫会更方便地与数据库交谈。

Complementary: 补充:

I suggest you to reading this tutorial about how to use SqlDataReader. 我建议你阅读教程,了解如何使用SqlDataReader。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM