如何使用Windows窗体应用程序中的存储过程从sql server绑定网格视图的数据 [英] How to bind data for grid view from sql server using store procedure in windows form application

查看:126
本文介绍了如何使用Windows窗体应用程序中的存储过程从sql server绑定网格视图的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我试图使用Windows应用程序的存储过程在网格视图中显示数据,但我被困在这里...

here is my code that i am trying to display the data in grid view using store procedure for windows application,but i got stuck here...

con = new SqlConnection("server=(local);database=sp;user id=sa;password =******");
           con.Open();
           cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText = "P_PB_Getall"; // store procedure name 
           dataGridView1.DataSource = ; // here what should i write the code to display  the data in the grid view... 

           con.Close();

推荐答案

试试这个 -

Try this-
con = new SqlConnection("server=(local);database=sp;user id=sa;password =******");
           con.Open();
           cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText = "P_PB_Getall"; 
           Datatable dt = new Datatable();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);
           dataGridView1.DataSource = dt; 
           con.Close();





希望,它有帮助:)



Hope, it helps :)


int id = Convert.ToInt32(comboBox2.SelectedValue.ToString());
    //int idc = 100;
    DataSet ptDataset = new DataSet();
    string con = ConfigurationManager.ConnectionStrings["secaloFormulaCS"].ToString(); 
    SqlConnection sqlCon = new SqlConnection(con);
    sqlCon.Open();
    SqlCommand sqlCmd = new SqlCommand("spDispInformation", sqlCon);
    sqlCmd.CommandType = CommandType.StoredProcedure;
    sqlCmd.Parameters.AddWithValue("@id", id);
    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
    da.Fill(ptDataset);
    dataGridView2.DataSource =  ptDataset.Tables[0];
    sqlCon.Close();


添加这个,



add this,

Datatable dt = new Datatable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
datagridview1.Bind();
con.close();


这篇关于如何使用Windows窗体应用程序中的存储过程从sql server绑定网格视图的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆