嘗試建立到本地SQL Server Express實例的連接,以便我可以在列表框中顯示列。構建運行良好,我看不到錯誤,但列表框中沒有數據。我測試了查詢,那很好。我正在使用NT身份驗證到數據庫。任何想法,我可能會出錯?通過ADO.NET連接到SQL Server - 空列表框
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"] + ")");
}
}
}
您的數據庫是** AdventureWorks2014 **,但您使用** Person.Person **。怎麼樣?那應該是** AdventureWorks2014.Person **。 – Berkay
@BerkayYaylaci表名沒有錯,OP指定表的模式。事實上,'AdventureWorks2014.Person'將是錯誤的,你必須鍵入'AdventureWorks2014.dbo.Person' –
@PanagiotisKanavos謝謝,好點。 – Berkay