我在C#里面添加了sqlConnection,sqlDataAdapter,DataSet,用DataGrid显示出来,运行后DataGrid中只有数据库里的字段名。没有数据值。试了好多次了还是不行,谁
能给我分析下。对了我机上装的是sql server2000

解决方案 »

  1.   

    你的结果集里肯定只有表头一条,你打开DATSET表自己看一下就知道了。
      

  2.   

    datagrid1.databind()web 里面要进行一次数据绑定,但在桌面程序中好像不要..
      

  3.   

    kook_tian说的对啊,datset只有表头一项,没有别的
      

  4.   

    WEB 里面要datagrid1.databind()Windows 就不要绑定了!
      
    是不是你数据库里表没有数据啊?自己看看啊?
      

  5.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication1
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.DataGrid dataGrid1;
    private System.Data.SqlClient.SqlConnection sqlConnection1;
    private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
    private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
    public WindowsApplication1.DataSet1 dataSet11;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.dataSet11 = new WindowsApplication1.DataSet1();
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
    this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "STUDENT";
    this.dataGrid1.DataSource = this.dataSet11;
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(0, 0);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.ReadOnly = true;
    this.dataGrid1.Size = new System.Drawing.Size(584, 328);
    this.dataGrid1.TabIndex = 0;
    // 
    // dataSet11
    // 
    this.dataSet11.DataSetName = "DataSet1";
    this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
    // 
    // sqlConnection1
    // 
    this.sqlConnection1.ConnectionString = "workstation id=\"BAIDAQIN-00CF91\";packet size=4096;user id=sa;data source=\"BAIDAQI" +
    "N-00CF91\";persist security info=False;initial catalog=master";
    this.sqlConnection1.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage);
    // 
    // sqlDataAdapter1
    // 
    this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
      new System.Data.Common.DataTableMapping("Table", "STUDENT", new System.Data.Common.DataColumnMapping[] {
     new System.Data.Common.DataColumnMapping("xingming", "xingming"),
     new System.Data.Common.DataColumnMapping("SEX", "SEX")})});
    // 
    // sqlInsertCommand1
    // 
    this.sqlInsertCommand1.CommandText = "INSERT INTO STUDENT(xingming, SEX) VALUES (@xingming, @SEX); SELECT xingming, SEX" +
    " FROM STUDENT";
    this.sqlInsertCommand1.Connection = this.sqlConnection1;
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@xingming", System.Data.SqlDbType.VarChar, 25, "xingming"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SEX", System.Data.SqlDbType.VarChar, 2, "SEX"));
    // 
    // sqlSelectCommand1
    // 
    this.sqlSelectCommand1.CommandText = "SELECT xingming, SEX FROM STUDENT";
    this.sqlSelectCommand1.Connection = this.sqlConnection1;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(544, 378);
    this.Controls.Add(this.dataGrid1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    } private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
    {

    }
    }
    }
      

  6.   

    private void Form1_Load(object sender, System.EventArgs e)
    {

    SqlConnection conn=new SqlConnection("database=pubs;uid=sa;pwd=;server=.");
    SqlDataAdapter Data=new SqlDataAdapter("select * from authors ",conn);
    DataSet DS=new DataSet();
    Data.Fill(DS,"jiaohuo");
    dataGrid1.DataSource=DS.Tables[0];
    }
    看看会不会来
      

  7.   

    最后一句应该这样写的吧
    dataGrid1.DataSource=DS.Tables[0].DefaultView;
      

  8.   

    我的问题解决,是一个大侠告诉我的。因为我的代码全是向导生成 的,
    private void Form1_Load(object sender, System.EventArgs e)
    {
    //加上:
             dataSet11.Clear();
    sqlDataAdapter1.Fill(dataSet11);

    }
    这就行了,呵呵,太高兴了