计算列调用存储过程

时间:2013-01-23 00:12:12

标签: c# datagridview

我希望你能伸出援助之手。

当该列的数据来自datagridview时,如何在storedProcedure中获取计算列。它只是与sp一起返回的一个值。我不确定这是否可行,或者我应该在sql字符串中包含一个函数sql来执行该任务?

1 个答案:

答案 0 :(得分:0)

如何以编程方式进行.. 你点击例如加载的按钮.. 这是加载存储过程的典型代码示例。  你可以将代码移动到page_load而不是Button1_click .. 我只是发布了重要的代码片段。

public partial class WebForm4 : System.Web.UI.Page {
SqlConnection sqlcon = new SqlConnection("Data Source=Local-host;Initial 
                                      Catalog=HRM_Login;Integrated Security=True");
        SqlCommand sqlcmd = new SqlCommand();
        SqlDataAdapter sqladp = new SqlDataAdapter();
        DataSet ds = new DataSet();

    protected void Button1_Click(object sender, EventArgs e)
            {
                Label2.Visible = true;
                GridView1.Visible = true;

                sqlcmd.Connection = sqlcon;
                sqlcmd.CommandText = "GetStudentName";
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add(new SqlParameter("@Studentid", SqlDbType.Int)).Value 
                                          = Int32.Parse(txt_studentid.Text);
                sqladp.SelectCommand = sqlcmd;
                sqladp.Fill(ds);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }
    }

well u can try
GridView1.DataSource = ds; instead of GridView1.DataSource = ds.Tables[0];
相关问题