简体   繁体   English

ASP.NET C#和存储过程

[英]ASP.NET C# & Stored Procedures

I am making the transition from DataTableAdapters in MSVS to creating Stored Procedures in MS SQL Server. 我正在从MSVS中的DataTableAdapters转换到在MS SQL Server中创建存储过程。

This is what I have so far. 这就是我到目前为止所拥有的。

protected void Submit_Click(object sender, EventArgs e)
    {
         var conString = ConfigurationManager.AppSettings["ConnectionString"].ToString(); 
         using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlCommand cmd = new SqlCommand("getAdministrators", con))
            {
                cmd.Parameters.Add("@userName", SqlDbType.VarChar).Value = userNameTB.Text;
                cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = passwordTB.Text;
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                }
            }
        }
    }

I want to grab all the rows/columns and store them in a DataSet and use the values returned at my will for whatever I wish. 我想获取所有行/列并将它们存储在DataSet中,并使用我想要的值返回我想要的任何值。 What am I missing to accomplish this. 我完成这件事我缺少什么。 Or if someone has an article that could help as well? 或者,如果某人有一篇文章可以提供帮助呢?

I updated the code to use using . 我更新为使用的代码using

I think the code is slowly getting there... 我认为代码正在慢慢地到达那里......

This snippet works: 此代码段有效:

    public static DataSet GetAll(int id)
    {
        using (SqlConnection con = new SqlConnection(Database.ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("sp_ContactGetAll", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataSet dsData = new DataSet();
                    da.Fill(dsData);
                    return dsData;
                }
            }
        }
    }

I may be wrong, but I would expect the following to work: 我可能错了,但我希望以下工作:

using(var reader = cmd.ExecuteReader()) {
  ds.Tables[0].Load(reader);
}

(assuming you want to populate the zeroth table) (假设您要填充第0个表)

http://msdn.microsoft.com/en-us/library/9kcbe65k.aspx http://msdn.microsoft.com/en-us/library/9kcbe65k.aspx

I guess you missed cmd.ExecuteReader 我猜你错过了cmd.ExecuteReader

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

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