日期:2014-05-18  浏览次数:20798 次

C#数据源问题
谁帮我看一下下面的代码有什么问题?点按钮后没反应

C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Ctest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private SqlConnection cnn;
        private SqlDataAdapter da;

      
        
        private void ok_Click(object sender, EventArgs e)
        {

            string strConnection = "user id=sa;password=123456;";
            strConnection += "initial catalog=stock;Server=(local);";
            strConnection += "Connect Timeout=30";

         cnn = new SqlConnection(strConnection);
            da = new SqlDataAdapter("select * from 物料清单",cnn);

            DataSet ds=new DataSet();
            
            DG.DataSource = ds;
            da.Fill(ds);
              
         
        
        }

           

      
            }
}



------解决方案--------------------


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Ctest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private SqlConnection cnn;
private SqlDataAdapter da;



private void ok_Click(object sender, EventArgs e)
{

string strConnection = "user id=sa;password=123456;";
strConnection += "initial catalog=stock;Server=(local);";
strConnection += "Connect Timeout=30";

cnn = new SqlConnection(strConnection);
da = new SqlDataAdapter("select * from 物料清单",cnn);

DataSet ds=new DataSet();
da.Fill(ds);
DG.DataSource = ds;




}




}
}

------解决方案--------------------
你的da.Fill(ds)怎么放在最后了. 把DG.DataSource = ds; da.Fill(ds);换个位置看看.

------解决方案--------------------
接分,呵呵,个人失误!如果先DG.DataSource = ds; 那么显示的就是空数据(无任何数据,也就没反映)


------解决方案--------------------
探讨
你的da.Fill(ds)怎么放在最后了. 把DG.DataSource = ds; da.Fill(ds);换个位置看看.

------解决方案--------------------
private void button2_Click(object sender, EventArgs e)
{
string sql = "select * from test";
string sqlconn = "data source=.;database=emgn_byang_sale;uid=sa;pwd=";
SqlConnection ss = new SqlConnection(sqlconn);
ss.Open();
SqlDataAdapter sd = new SqlDataAdapter(sql, ss);
DataTable dt = new DataTable();
sd.Fill(dt);
dataGridView1.DataSource = dt;
}
刚才没仔细看,好象你的连接没OPEN和close嘛!
------解决方案--------------------
da = new SqlDataAdapter("select * from 物料清单",cnn);

DataSet ds=new DataSet();

DG.DataSource = ds;
da.Fill(ds);