使用for語句,遍歷表的某一列,然后顯示在組合框中!
2011-05-14 15:51 推薦: 0 次 有幫助?
請推薦 讀數(shù)據(jù)庫會吧,循環(huán)取,添加到下拉
2011-05-14 15:52 推薦: 0 次 有幫助?
請推薦 - C# code
List<string> list=new List();//循環(huán)遍歷讀取數(shù)據(jù)庫將表中字段//再添加到集合中while(read.Read()){ list.Add(read["字段名"]);}return list;//再將數(shù)據(jù)源綁定就行了this.cboText.DataSource=list;
2011-05-14 16:05 推薦: 0 次 有幫助?
請推薦 - C# code
//讀取時要轉化list.Add(read["字段名"].toString());
2011-05-14 16:06 推薦: 0 次 有幫助?
請推薦 2011-05-14 19:59 推薦: 0 次 有幫助?
請推薦 謝謝各位??!還是不太會,代碼能在具體點嗎?我實在是基礎太差了。假如我建的數(shù)據(jù)庫名叫分銷系統(tǒng),它下面的一個表叫客戶資料,表的列分別為:客戶編碼、客戶名稱、電話和地址。如何將此表中的列(客戶名稱)顯示在一個窗體里的組合框控件的下拉列表中?
2011-05-14 20:35 推薦: 0 次 有幫助?
請推薦 - C# code
IList<string> lists = new List<string>(); lists.Add("aaa"); lists.Add("bbb"); lists.Add("ccc"); this.comboBox1.DataSource = lists;
2011-05-14 20:41 推薦: 0 次 有幫助?
請推薦 - C# code
IList<string> lists = new List<string>(); lists.Add("aaa"); lists.Add("bbb"); lists.Add("ccc"); foreach (var item in lists) { this.comboBox1.Items.Add(item); }
2011-05-14 20:42 推薦: 0 次 有幫助?
請推薦 using System.Data;
using System.Data.SqlClient;
- C# code
public string connStr = "server=.;uid=用戶名;pwd=密碼;database=你的數(shù)據(jù)庫名;"; private void Form5_Load(object sender, EventArgs e) { BindComboBox(); }private void BindComboBox() { string sql = "select 客戶編碼,客戶名稱,電話,地址 from 客戶資料"; comboBox1.DataSource = getDT(sql); comboBox1.DisplayMember = "客戶名稱"; //選項顯示內容 comboBox1.ValueMember = "客戶編碼"; //選項對應的value label1.Text = "總記錄數(shù):" + comboBox1.Items.Count; } private DataTable getDT(string sql) { DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); SqlDataAdapter sda = new SqlDataAdapter(sql, conn); sda.Fill(dt); } return dt; }
2011-05-14 21:16 推薦: 0 次 有幫助?
請推薦 先寫SQL查詢語句 select 列名(你想填充至combobox的列)from 表名
之后再去數(shù)據(jù)庫讀取
創(chuàng)建dataRead 對象 具體是哪個dataRead對象要看你用的是哪種數(shù)據(jù)庫 此處我用MS的SQL server 2008
那就是創(chuàng)建一個sqldataRead
- C# code
SqldataRead read=cmd.ExecuteReader();while(read.read()){ this.combobox.items.Add(read["列名"].ToString);//此處為用ADD方法直接往控件里添加內容,}
2011-05-14 21:35 推薦: 0 次 有幫助?
請推薦 更正:上貼中sqldataRead應改為SqldataReader ,在此道歉,手誤!見諒?。?
2011-05-14 21:38 推薦: 0 次 有幫助?
請推薦 回復于:2011-05-13 22:16:45
using System.Data;
using System.Data.SqlClient;
C# code
public string connStr = "server=.;uid=用戶名;pwd=密碼;database=你的數(shù)據(jù)庫名;";
private void Form5_Load(object sender, EventArgs e)
{
BindComboBox();
}
private void BindComboBox()
{
string sql = "select 客戶編碼,客戶名稱,電話,地址 from 客戶資料";
comboBox1.DataSource = getDT(sql);
comboBox1.DisplayMember = "客戶名稱"; //選項顯示內容
comboBox1.ValueMember = "客戶編碼"; //選項對應的value
label1.Text = "總記錄數(shù):" + comboBox1.Items.Count;
}
private DataTable getDT(string sql)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
sda.Fill(dt);
}
return dt;
}
9樓,太感謝你了,很詳細。我試試。
2011-05-15 19:53 推薦: 0 次 有幫助?
請推薦 謝謝,各位高手的指點啊。我都試試,看可以不。
2011-05-15 19:54 推薦: 0 次 有幫助?
請推薦 9樓的大俠,把你發(fā)的那個代碼中的public string connStr = "server=.;uid=用戶名;pwd=密碼;database=你的數(shù)據(jù)庫名;";寫成string connString = @"server=PC200032619QHN\EXPRESS;integrated security=true;DataBase=分銷系統(tǒng);";可以不?謝謝!
2011-05-17 16:22 推薦: 0 次 有幫助?
請推薦 應該不可以吧,我沒這樣寫過。不過我知道你的意思。你可以這樣寫:
string connString=“Data Source=PC200032619QHN\EXPRESS; Initial Catalog=分銷系統(tǒng);Integrated Security=True"
2011-05-17 16:49 推薦: 0 次 有幫助?
請推薦 謝謝十五樓啊,我試試。
2011-05-17 19:18 推薦: 0 次 有幫助?
請推薦 還是不行,老在 conn.open()那個地方提示:在建立與服務器的連接時出錯。在連接到 SQL Server 2005 時,在默認的設置下 SQL Server 不允許進行遠程連接可能會導致此失敗。 (provider: SQL 網(wǎng)絡接口, error: 26 - 定位指定的服務器/實例時出錯)
2011-05-17 19:23 推薦: 0 次 有幫助?
請推薦 用這種方式:public string connStr = "server=.;uid=用戶名;pwd=密碼;database=你的數(shù)據(jù)庫名;";也是提示像上面那樣的錯誤。我數(shù)據(jù)庫里面該開的也都開了啊。還是不行。
2011-05-17 19:25 推薦: 0 次 有幫助?
請推薦 你的數(shù)據(jù)庫之前可以嗎?就是連接別的項目行不行,用windows身份驗證登錄的話正不正常
2011-05-17 20:54 推薦: 0 次 有幫助?
請推薦 也可用datagridview實現(xiàn),就是不知道你解決了沒有。。。
2011-05-17 21:01 推薦: 0 次 有幫助?
請推薦 從數(shù)據(jù)庫查詢實體類,返回DataSet ds ;
this.Combox1.DataSource = ds;
this.Combox1.DisplayMember = "列名";
2011-05-17 21:28 推薦: 0 次 有幫助?
請推薦 10樓:ProgramingCreazy
我按你的方法做了,調試時出現(xiàn)兩個錯誤:
錯誤 1 找不到類型或命名空間名稱“SqldataReader”(是否缺少 using 指令或程序集引用?)
錯誤 2 “System.Windows.Forms.ComboBox”不包含“items”的定義,并且找不到可接受類型為
“System.Windows.Forms.ComboBox”的第一個參數(shù)的擴展方法“items”(是否缺少 using 指令或程序集引用?)
麻煩你指點一下是怎么回事?謝謝你?。?br>我的代碼是:
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 組合框顯示
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string connString = @"server=PC201105219JN;uid=sa,pwd=123;database=fjf;";
string sql = @"select 客戶編碼,客戶名稱 from 客戶資料";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
SqldataReader read = cmd.ExecuteReader();
while (read.read())
{
this.國家列表.items.Add(read["客戶名稱"].ToString);//此處為用ADD方法直接往控件里添加內容,
}
}
}
}
2011-05-22 17:21 推薦: 0 次 有幫助?
請推薦 SqlDataReader read = cmd.ExecuteReader();
SqldataReader的data那個D要大寫
this.國家列表.items.Add(read["客戶名稱"].ToString;
一樣的問題items的i要大寫
while (read.read())
一樣read()的r也要大寫
你都是硬打出來的么?
2011-05-23 00:32 推薦: 0 次 有幫助?
請推薦 還有你從頭到尾都沒打開過數(shù)據(jù)庫
SqlCommand cmd = new SqlCommand(sql, conn);
下面加上
conn.Open();
2011-05-23 00:35 推薦: 0 次 有幫助?
請推薦 private void Form1_Load(object sender, EventArgs e)
{
string connString = @"server=PC201105219JN;uid=sa,pwd=123;database=fjf;";
string sql = @"select 客戶編碼,客戶名稱 from 客戶資料";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader read = cmd.ExecuteReader();
while (read.Read())
{
this.國家列表.Items.Add(read["客戶名稱"].ToString);//此處為用ADD方法直接往控件里添加內容,
}
read.Close();
conn.Close();
}
2011-05-23 00:39 推薦: 0 次 有幫助?
請推薦 謝謝25樓啊,我剛學,編程基礎也差,見笑了。呵呵。。。
2011-05-24 08:16 推薦: 0 次 有幫助?
請推薦