System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=194910");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from customers",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"customers");ComboBox1.DataSource = dt.Tables[0];
ComboBox1.DataMemeber =
ComboBox1.DisplayValue =
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from customers",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"customers");ComboBox1.DataSource = dt.Tables[0];
ComboBox1.DataMemeber =
ComboBox1.DisplayValue =
我的代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace WindowsApplication3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.DataSet ds;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
//设置连接字符串
string myConnectionString;
myConnectionString="Initial Catalog=oscar;Connect Timeout=15;"+
"Data Source=192.168.0.144;user id=sa;password=801224";
//新建一个连接对象
sqlConnection1=new SqlConnection(myConnectionString);
try
{
//建立连接
sqlConnection1.Open();
sqlDataAdapter1 =new SqlDataAdapter("SELECT 用户名 FROM users",sqlConnection1);
ds=new DataSet();
sqlDataAdapter1.Fill(ds,1,0,"用户名");
comboBox1.DataSource = ds.Tables[0];
}
catch(SqlException)
{
//弹出对话框提示登录失败
MessageBox.Show("出错,不能连接到服务器!","提示");
this.Text="登录数据库失败,请检查服务器区内的资料是否输入正确!";
}
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(80, 80);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.comboBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
但还是调不到里面的内容:
comboBox里面只显示“System.Data.DataRowView”.请指导
等待回复
DataSet ds=new DataSet("XMLProducts");
//连接SQL Server数据库
SqlConnection conn=new SqlConnection(@"server=glf;uid=sa;pwd=;database=northwind");
SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM products",conn);
//绑定DataGrid控件
da.Fill(ds,"products");
comboBox1.DisplayMember="显示的字段名";
comboBox1.ValueMember="对应值的字段名";