通过 ADO.NET 连接到 SQL 服务器 - 空列表框

Connection to SQL Server through ADO.NET - Empty Listbox

正在尝试建立与本地 SQL Server Express 实例的连接,以便我可以在列表框中显示列。构建运行良好,我看不到错误,但列表框中没有数据。我已经测试了查询,这很好。我正在对数据库使用 NT 身份验证。有什么我可能出错的想法吗?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void customers_SelectedIndexChanged(object sender, EventArgs e)
    {
        string commstring = "Driver ={SQL Server}; Server = DESKTOP-5T4MHHR\SQLEXPRESS; Database = AdventureWorks2014; Trusted_Connection = Yes;";
        string connstring = "SELECT FirstName, LastName FROM Person.Person";

        SqlDataAdapter customerDataAdapater = new SqlDataAdapter(commstring, connstring);

        DataSet customerDataSet = new DataSet();
        customerDataAdapater.Fill(customerDataSet, "Person.Person");

        DataTable customerDataTable = new DataTable();
        customerDataTable = customerDataSet.Tables[0];

        foreach (DataRow dataRow in customerDataTable.Rows)
        {
            customers.Items.Add(dataRow["FirstName"] + " (" + dataRow["LastName"] + ")");
        }
    }
}

您的连接字符串似乎很奇怪.....

你能试试只用这个吗:

string commstring = "Server=DESKTOP-5T4MHHR\SQLEXPRESS;Database=AdventureWorks2014;Trusted_Connection=Yes;";

另外:为什么要先创建一个DataSet,填入一组数据,然后从中提取一个DataTable??这是不必要的复杂代码 - 只需使用它:

SqlDataAdapter customerDataAdapater = new SqlDataAdapter(commstring, connstring);

// if you only ever need *one* set of data - just use a DataTable directly!
DataTable customerDataTable = new DataTable();

// Fill DataTable with the data from the query
customerDataAdapater.Fill(customerDataTable);

更新:我真的会把你的代码重写成这样:

// create a separate class - call it whatever you like
public class DataProvider
{
    // define a method to provide that data to you
    public List<string> GetPeople()
    {
        // define connection string (I'd really load that from CONFIG, in real world)
        string connstring = "Server=MSEDTOP;Database=AdventureWorks2014;Trusted_Connection=Yes;";

        // define your query
        string query = "SELECT FirstName, LastName FROM Person.Person";

        // prepare a variable to hold the results
        List<string> entries = new List<string>();

        // put your SqlConnection and SqlCommand into "using" blocks
        using (SqlConnection conn = new SqlConnection(connstring))
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            conn.Open();

            // iterate over the results using a SqlDataReader
            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    // get first and last name from current row of query
                    string firstName = rdr.GetFieldValue<string>(0);
                    string lastName = rdr.GetFieldValue<string>(1);

                    // add entry to result list
                    entries.Add(firstName + " " + lastName);
                }

                rdr.Close();
            }

            conn.Close();
        }

        // return the results
        return entries;
    }
}

在你的代码隐藏中,你只需要做这样的事情:

protected override void OnLoad(.....)
{
    if (!IsPostback)
    {
        List<string> people = new DataProvider().GetPeople();

        customers.DataSource = people;
        customers.DataBind();
    }
}

但我仍然不明白当 SelectedIndexChanged 事件发生时您在尝试什么......

我在此处的示例项目中测试了您的代码,发现您以错误的顺序将参数传递给 SqlDataAdapter 构造函数。

更改以下行后:

SqlDataAdapter customerDataAdapater = new SqlDataAdapter(commstring, connstring);

来自

SqlDataAdapter customerDataAdapater = new SqlDataAdapter(connstring, commstring);

列表框填写成功。