简体   繁体   English

我必须在aspx中创建数据源吗? 我可以在C#中执行sqldatasource(sql SPROC),参数和databind吗

[英]Do I have to create a datasource in aspx? Can I do sqldatasource (sql SPROC), parameters and databind in C#

First, I'll apologize for a newbie question, but I can't seem to find a good example of what I need to do. 首先,我会为一个新手问题道歉,但是我似乎找不到一个很好的例子说明我需要做什么。 I have been having trouble with datatypes and my stored procedures when I attempt to create a sqlDataSource in my aspx page and assign session parametersof type int. 尝试在aspx页中创建sqlDataSource并分配int类型的会话参数时,我在处理数据类型和存储过程时遇到了麻烦。 I wanted to try to do the whole thing in the codebehind so it is easier to debug. 我想尝试在后面的代码中完成整个操作,因此更容易调试。 It just seems very confusing to me to do part of the work in the aspx, and part in the codebehind. 对我来说,在aspx中完成部分工作,在背后的代码中进行部分工作,似乎让我感到困惑。 I would be interested in hearing oppinions on this, but I have code here that shows the current state of my attempt. 我希望听到对此的意见,但是我这里的代码显示了我的尝试的当前状态。 Could you possibly help me get it working? 你能帮我使它工作吗?

protected void DoReport()
{
  int ClinicID = Convert.ToInt32(Session["selectedClinic"]);
  String connstr = System.Configuration.ConfigurationManager.ConnectionStrings 
        ["PC3PaymentConnection"].ConnectionString;
       using (SqlConnection conn = new SqlConnection(connstr))
       {
       using ( SqlCommand cmdMeasureHist = new SqlCommand("GetMeasureDetailHistory", conn))
        {
        cmdMeasureHist.CommandType = CommandType.StoredProcedure;
        SqlParameter pclinic = cmdMeasureHist.Parameters.Add("@ClinicID", SqlDbType.Int);
        pclinic.Value = ClinicID;
        SqlParameter pCMSMID = cmdMeasureHist.Parameters.Add("@CMMeasureID", SqlDbType.Int);
        pCMSMID.Value = Convert.ToInt32(ddMeasures.SelectedValue);
        SqlDataSource DsMeasureHist = new SqlDataSource();
        gvHistory.DataSourceID = "DsMeasureHist";
        conn.Open();
        DsMeasureHist.ExecuteNonQuery();
  gvHistory.DataBind();
   }

I just don't understand how to hook the command to the datasource to the gridview. 我只是不明白如何将命令挂接到网格视图的数据源。 Please help! 请帮忙!

You need to use SqlDataAdapter for this, ExecuteNonQuery() is usually for executing insert,update,delete commands. 您需要为此使用SqlDataAdapter,ExecuteNonQuery()通常用于执行insert,update,delete命令。 If you want a datasource you'll either use DataReader or DataAdapter. 如果需要数据源,则可以使用DataReader或DataAdapter。 Check the following example: 检查以下示例:

using ( SqlCommand cmdMeasureHist = new SqlCommand("GetMeasureDetailHistory", conn))
        {
        cmdMeasureHist.CommandType = CommandType.StoredProcedure;
        SqlParameter pclinic = cmdMeasureHist.Parameters.Add("@ClinicID", SqlDbType.Int);
        pclinic.Value = ClinicID;
        SqlParameter pCMSMID = cmdMeasureHist.Parameters.Add("@CMMeasureID", SqlDbType.Int);
        pCMSMID.Value = Convert.ToInt32(ddMeasures.SelectedValue);

SqlDataAdapter da = new SqlDataAdapter(cmdMeasureHist); DataTable dt = new DataTable(); da.Fill(dt); gvHistory.DataSource = dt; gvHistory.DataBind();

Here is an example of how to define everything in the SQLDataSource. 这是一个如何在SQLDataSource中定义所有内容的示例。 You'll probably need to fine tune this a bit more but hopefully it will get you in the right direction. 您可能需要对此进行一些微调,但希望它将使您朝正确的方向发展。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:PC3PaymentConnection %>" 
        SelectCommand="GetMeasureDetailHistory"
SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter Name="CMMeasureID"  ControlID="ddMeasures" 
PropertyName="SelectedValue" Type="Int32" />            
            <asp:SessionParameter Name="ClinicID" 
SessionField="selectedClinic" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM