我如何...在Windows应用程序的c#中绑定Sql Server 2008的下拉列表 [英] How do I.. bind drop down list from Sql Server 2008 in c# in windows application
问题描述
如何在Windows应用程序的c#中绑定来自Sql Server 2008的下拉列表
请参阅下面的链接或代码希望这些帮助.....
在Form1.cs中编写代码
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Data.SqlClient;
命名空间 BindComboBox
{
public 部分 类 Form1:表格
{
public Form1()
{
InitializeComponent();
}
私有 void Form1_Load( object sender,EventArgs e)
{
loadCountry();
}
public void loadCountry()
{
DataTable dt = new DataTable();
尝试
{
使用(SqlConnection con = new SqlConnection( 数据源= testdb;初始目录= ABC; Uid = sa;密码= PassWord123;))
{
使用(SqlDataAdapter adp = new SqlDataAdapter( SELECT * FROM country,con))
{
adp.Fill(dt);
comboBox1.DisplayMember = Country;
comboBox1.ValueMember = Id;
comboBox1.DataSource = dt;
}
}
}
catch (例外情况)
{
throw ex;
}
}
}
}
http://www.dotnetsharepoint.com/2013/08/how-to-bind-data-in-combobox-from.html [ ^ ]
http://csharp.net-informations.com/dataset/dataset-combobox.htm [ ^ ]
http://www.aspdotnet-suresh.com/2012/10/showbind-data-to-aspnet-dropdownlist.html [< a href =http://www.aspdotnet-suresh.com/2012/10/showbind-data-to-aspnet-dropdownlist.htmltarget =_ blanktitle =New Window> ^ ]
Happy Co丁...
how to bind drop down list from Sql Server 2008 in c# in windows application
Refer below links or Code hope these help.....
Writing the code in Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace BindComboBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { loadCountry(); } public void loadCountry() { DataTable dt = new DataTable(); try { using (SqlConnection con = new SqlConnection("Data Source = testdb;Initial Catalog = ABC;Uid = sa;Password = PassWord123;")) { using (SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM country", con)) { adp.Fill(dt); comboBox1.DisplayMember = "Country"; comboBox1.ValueMember = "Id"; comboBox1.DataSource = dt; } } } catch (Exception ex) { throw ex; } } } }
http://www.dotnetsharepoint.com/2013/08/how-to-bind-data-in-combobox-from.html[^]
http://csharp.net-informations.com/dataset/dataset-combobox.htm[^]
http://www.aspdotnet-suresh.com/2012/10/showbind-data-to-aspnet-dropdownlist.html[^]
Happy Coding...
这篇关于我如何...在Windows应用程序的c#中绑定Sql Server 2008的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!