C# WinForm ListBox 绑定数据库代码

private void Form1_Load(object sender, EventArgs e)
        {
            string sqlString = "SELECT DISTINCT process FROM monFile";
            DataSet ds = GetData(sqlString);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listBox1.Items.Add(row["process"].ToString());
            }
        }

        DataSet GetData(String queryString)
        {
            using (SqlConnection conn = new SqlConnection("Data Source=SIYUAN\\SQLEXPRESS; Initial Catalog=monitor; User ID=siyuan; Pwd=123456"))
            {
                DataSet ds = new DataSet();
                try
                {
                    // Connect to the database and run the query.
                    conn.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(queryString, conn);
                    // Fill the DataSet.
                    adapter.Fill(ds);
                }
                catch (Exception ex)
                {
                    // The connection failed. Display an error message.
                    label1.Text = "Unable to connect to the database.";
                }
                return ds;
            }

        }


》》点击查看原文

你可能感兴趣的:(数据库,exception,String,C#,WinForm,dataset)