【问题标题】:A good way to access stored procedure on SQL Server 2008 from C#从 C# 访问 SQL Server 2008 上的存储过程的好方法
【发布时间】:2009-08-03 19:52:43
【问题描述】:

我正在尝试从我的 ASP.NET / C# Web 应用程序访问存储过程。

有没有特别好的方法(也是传参数的好方法)?

我遵循以下步骤,绝对不喜欢这种方法,因为您将过程作为字符串输入(不可调试):http://www.c-sharpcorner.com/UploadFile/dclark/InsOutsinCS11302005072332AM/InsOutsinCS.aspx

【问题讨论】:

  • 你没有仔细阅读那篇文章。您不要将过程作为字符串输入,只输入它的名称。

标签: c# asp.net ado.net


【解决方案1】:

如果您要使用 LinqToSQL,ScottGU 在该主题上有一些出色的 blog posts

【讨论】:

    【解决方案2】:

    我喜欢SubSonic。 SubSonic 将根据您的数据库架构为您生成 DAL。大多数人使用生成的带有 activerecord 或 simplerepository 模式的 SubSonic 对象来处理数据库,但 Subsonic 还生成强类型方法来包装您的存储过程和视图。

    这是一个相当基本的示例,但您可以进行类似于以下的存储过程调用:

    SubSonic.StoredProcedure sp = Dal.StoredProcedures.UpdatePassword(id, pw);
    DataSet ds = sp.GetDataSet();
    

    在上面的代码中,StoredProcedures 类包括一个用于 UpdatePassword 存储过程的强类型方法。它对我很有用。

    SubSonic Docs are here.

    【讨论】:

      【解决方案3】:

      使用 LINQ to SQL 或 Entity Framework 确实为您提供了对参数和结果的良好强类型访问,但在使用它们之前需要考虑很多事项。 LINQ to SQL 已停产,Entity Framework 将在 .NET Framework 4.0 中进行重大改革。

      为了做出明智的考虑,我想我会分享低级别的 ADO.NET 做事方式:

      void CallMyStoredProcedure(string connectionString, string paramValue)
      {
          string spName = "MyStoredProcedure";
          string columnValue;
      
          using (SqlConnection conn = new SqlConnection(connectionString))
          using (SqlCommand cmd = new SqlCommand(spName, conn))
          {
              cmd.CommandType = CommandType.StoredProcedure;
      
              // Set parameter values
              cmd.Parameters.AddWithValue("@inputParam",
                  (object)paramValue ?? DBNull.Value);
      
              // Parameter to store return value of your SP
              SqlParameter returnParam = cmd.Parameters.Add(
                  new SqlParameter("RETURN_VALUE", SqlDbType.Int));
              returnParam.Direction = ParameterDirection.ReturnValue;
      
              // Execute SP and read results
              conn.Open();
              using (SqlDataReader dr = cmd.ExecuteReader())
              {
                  // Do something with return value
                  int? returnValue = cmd.Parameters["RETURN_VALUE"].Value as int?;
      
                  while (dr.Read())
                  {
                      // Suggested way to read results if you use
                      // "SELECT * FROM" in your SP. If you return columns
                      // in a fixed order, it's fairly safe to be reading
                      // in that order without looping
                      for (int field = 0; field < dr.FieldCount; field++)
                      {
                          switch (dr.GetName(field))
                          {
                              case "StringColumn":
                                  columnValue = dr.GetValue(field) as string;
                                  break;
                              default:
                                  // Column not recognized by your code.
                                  // You might choose to complain about it here.
                                  break;
                          }
                      }
                  }
              }
          }
      }
      

      是的,这是相当多的代码,所以如果你想使用 vanilla ADO.NET,你可能需要编写一些辅助方法。

      【讨论】:

        【解决方案4】:

        最好的方法是使用 LINQ to SQL 或 ADO.NET Entities。 Visual Studio 将为您的存储过程生成相应的强类型方法。

        【讨论】:

        • 你有任何方法/方法或教程来完成这个吗?
        猜你喜欢
        • 1970-01-01
        • 2011-01-31
        • 1970-01-01
        • 2015-01-26
        • 1970-01-01
        • 1970-01-01
        • 2015-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多