3

我对存储过程有疑问。

此代码有效(带有ListBox

private void button4_Click(object sender, EventArgs e)
{
   string connectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
   SqlConnection connection = new SqlConnection(connectionString);

   string sqlCmd = "Drie duurste producten";

   SqlCommand cmd = new SqlCommand(sqlCmd, connection);

   cmd.CommandType = CommandType.StoredProcedure;
   cmd.CommandText = sqlCmd;

   connection.Open();

   using (SqlDataReader reader = cmd.ExecuteReader())
   {
      while (reader.Read())
      {
          listBox1.Items.Add(reader.GetValue(0).ToString()); 
      }
   }   

   connection.Close();
}

但是如何将此数据添加到 aDataGridView而不是 a ListBox

谢谢!

4

7 回答 7

3

改成

   ......
   using (SqlDataAdapter adapter = new SqlDataAdapter()) 
   { 
        DataTable dt = new DataTable();
        adapter.SelectCommand = cmd;            { 
        adapter.Fill(dt);
        dataGridView1.DataSource = dt;
   }    
   ......

通常一个 DataGridView 被填充绑定一个完整的数据源到它的 DataSource 属性并让控件弄清楚如何配置它的列和显示的值的格式

于 2012-10-20T14:09:11.383 回答
1

SqlDataAdapter 是最简单的方法。

但也可以创建 DataTable 并手动填充它并将 DataGridView 的 DataSource 值分配给 DataTable 实例:

        ...

        DataTable dt = new DataTable("test");
        dt.Columns.Add("test");

        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                DataRow dr = dt.NewRow();
                dr[0] = reader.GetValue(0).ToString();
                dt.Rows.Add(dr);
            }
        }  

        dataGridView1.DataSource = dt;

        ....
于 2012-10-21T16:30:31.067 回答
1
    static public long Insert(BillAO ao)
    {
        try
        {
            SqlParameter[] Params =
            {
                new SqlParameter("@Status",ao.Status)
                , new SqlParameter("@BAID",ao.BAID)
                , new SqlParameter("@PhieuKhamID",ao.PhieuKhamID)
                , new SqlParameter("@ThuNganID",ao.ThuNganID)
                , new SqlParameter("@Ngay",ao.Ngay)
                , new SqlParameter("@SoTien",ao.SoTien)
                , new SqlParameter("@LyDo",ao.LyDo)
                , new SqlParameter("@GhiChu",ao.GhiChu)
                , new SqlParameter("@CreatedBy",ao.CreatedBy)
                , new SqlParameter("@CreatedTime",ao.CreatedTime)
                , new SqlParameter("@LastModifiedBy",ao.LastModifiedBy)
                , new SqlParameter("@LastModifiedTime",ao.LastModifiedTime)
            };
            int result = int.Parse(SqlHelper.ExecuteScalar(HYPO.Utils.Config.ConnString, CommandType.StoredProcedure, "SP_Bill_Insert", Params).ToString());
            return result;
        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("duplicate"))
            {
                return -2;
            }
            return -1;
        }
    }
于 2013-04-04T04:27:24.420 回答
0

您需要使用 SqlDataAdapter 来获取数据表中存储过程的结果。

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;      
da.Fill(dt);
dataGridView1.DataSource = dt;
于 2012-10-20T14:07:56.560 回答
0

你不需要 a CommandReader,你所要做的就是使用DataAdapterand DataSet。并将数据集绑定到您的DataGridView

DataSet ds = new DataSet();

using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
    adapter.Fill(ds);
    dataGridView1.DataSource = ds.Tables[0];
}
于 2012-10-20T14:09:58.327 回答
0

您可以使用 DataTable 或 DataSet 来填充数据...。这里我使用 DataTable ....

Datatable data = new Datatable();
using (SqlDataAdapter adp = new SqlDataAdapter())
{
    adp.SelectCommand = cmd;
    adp.Fill(data);
    GridView1.DataSorce = data;
    GridView1.DataBind();        <--- Needed to bind GridView at a time While Filling DataTable data
}  

在将DataTable分配给Gridview1之前,您也可以通过这种方式检查DataTable是否包含数据......

if(data.Rows.Counnt > 0)
{
        GridView1.DataSorce = data;
        GridView1.DataBind();     
}
于 2012-10-21T07:10:18.740 回答
0
public void whateverToolStripMenuItem_Click(object sender, EventArgs e) {
// A previously declared and instantiated OpenFileDialog, i put it from Design Mode, but you can just
// declare it as 
OpenFileDialog dlgImport = new OpenFileDialog();
//We show the dialog:
dlgImport.ShowDialog();
// We declare a variable to store the file path and name:
string fileName = dlgImport.FileName;
try {
    // We invoke our method, wich is created in the following section, and pass it two parameters
    // The file name and .... a DataGridView name that we put is the Form, so we can also see what
    // We imported. Cool, isn't it?
    importExcel(fileName, gridMain);
}
// It is best to always try to handle errors, you will se later why it is OleDbException and not
catch (OleDbException ex) {
    MessageBox.Show("Error ocurred: " + ex.Message);
}

}

于 2013-04-10T07:38:05.033 回答