[英]Connection to SQL Server through ADO.NET - Empty Listbox
Trying to set up a connection to my local SQL Server Express instance so that I can display columns in a listbox. 尝试建立与本地SQL Server Express实例的连接,以便可以在列表框中显示列。 Th build runs fine and I can't see errors, but there is no data in the listbox. 该版本运行正常,我看不到错误,但列表框中没有数据。 I have tested the query and that is fine. 我已经测试了查询,这很好。 I am using NT Authentication to the database. 我正在对数据库使用NT身份验证。 Any ideas where I might have gone wrong? 有什么想法我可能出错了吗?
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"] + ")");
}
}
}
Your connection string seems weird..... 您的连接字符串看起来很奇怪.....
Could you try using just this: 您可以尝试使用以下方法:
string commstring = "Server=DESKTOP-5T4MHHR\\SQLEXPRESS;Database=AdventureWorks2014;Trusted_Connection=Yes;";
Also: why are you first creating a DataSet
, filling in a single set of data, and then extracting a DataTable
from it?? 另外:为什么首先创建一个DataSet
,填充一组数据,然后从中提取一个DataTable
? This is unnecessarily complicated code - just use this instead: 这是不必要的复杂代码-只需使用以下代码即可:
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);
Update: I would really rewrite your code to something like this: 更新:我真的会将您的代码重写为如下形式:
// 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;
}
}
And in your code-behind, you only need to do something like this: 并且在您的代码隐藏中,您只需要执行以下操作:
protected override void OnLoad(.....)
{
if (!IsPostback)
{
List<string> people = new DataProvider().GetPeople();
customers.DataSource = people;
customers.DataBind();
}
}
but I still don't understand what you were trying to when the SelectedIndexChanged event happens..... 但是我仍然不明白您在SelectedIndexChanged事件发生时想要做什么。
I tested your code in a sample project here and I realized you passed the parameters to SqlDataAdapter constructor in a wrong order. 我在这里的示例项目中测试了您的代码,并且意识到您以错误的顺序将参数传递给了SqlDataAdapter构造函数。
After changing the follow line: 更改以下行之后:
SqlDataAdapter customerDataAdapater = new SqlDataAdapter(commstring, connstring);
by 通过
SqlDataAdapter customerDataAdapater = new SqlDataAdapter(connstring, commstring);
the listbox was filled successfully. 列表框已成功填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.