怎么在combobox添加Access数据库文件 5
代码是这样的运行不起来不知道为什么privatevoidForm1_Load(objectsender,EventArgse){try{stringCString="Pr...
代码是这样的运行不起来不知道为什么
private void Form1_Load(object sender, EventArgs e)
{
try
{
string CString = "Provider=Microsoft.Jet.OleDB.4.0;Data Source =khgl.mdb";
OleDbConnection conn = new OleDbConnection(CString);
conn.Open();
string sql = "select AreaName from tb_Area";
OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, "Area");
comboBox2.DataSource = ds.Tables["Area"].DefaultView;
comboBox2.DisplayMember = "AreaName";
comboBox2.ValueMember = "AreaName";
conn.Close();
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
} 展开
private void Form1_Load(object sender, EventArgs e)
{
try
{
string CString = "Provider=Microsoft.Jet.OleDB.4.0;Data Source =khgl.mdb";
OleDbConnection conn = new OleDbConnection(CString);
conn.Open();
string sql = "select AreaName from tb_Area";
OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, "Area");
comboBox2.DataSource = ds.Tables["Area"].DefaultView;
comboBox2.DisplayMember = "AreaName";
comboBox2.ValueMember = "AreaName";
conn.Close();
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
} 展开
1个回答
2015-05-14 · 知道合伙人互联网行家
关注
展开全部
Dim db As New ADODB.Connection
Dim rs As New ADODB.Recordset
db.Open "数据库连接字符“
If rs.State <> adStateClosed Then rs.Close
rs.Open "select * from 计量单位表", db, 1, 3
If rs.EOF = False And rs.BOF = False Then '下拉菜单里面的内容用计量单位表中的名称
rs.MoveFirst
While Not rs.EOF
Combo1.AddItem rs.Fields("显示的名称")
rs.MoveNext
Wend
End If
放在formload里面
Dim rs As New ADODB.Recordset
db.Open "数据库连接字符“
If rs.State <> adStateClosed Then rs.Close
rs.Open "select * from 计量单位表", db, 1, 3
If rs.EOF = False And rs.BOF = False Then '下拉菜单里面的内容用计量单位表中的名称
rs.MoveFirst
While Not rs.EOF
Combo1.AddItem rs.Fields("显示的名称")
rs.MoveNext
Wend
End If
放在formload里面
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询