【问题标题】:Update DB using stored procedure and ADO.NET使用存储过程和 ADO.NET 更新数据库
【发布时间】:2012-03-15 02:01:09
【问题描述】:

我想我在课堂上遗漏了一个“USING”语句,因为当我尝试将 commandType 设置为存储过程时出现错误。当我键入“cmd.CommandType =”时,Intellisense 无法找到'CommandType.StoredProcedure(注意:该功能只是部分粗略)。提前致谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;


namespace LegacyForms.Personal
{
    public partial class FormBuilder : System.Web.UI.Page
    {
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //Get the DB connection:
            string ConnString = ConfigurationManager.AppSettings["AssociatedBank2011ConnectionString"];
            SqlConnection conn = new SqlConnection(ConnString);



        SqlCommand cmd = new SqlCommand("uspInsertPersonalAccountApplcation", conn);
        cmd.Commandtype = **get error here!**
        cmd.Parameters.AddWithValue("@AccountType", AcctType);
        cmd.Parameters.AddWithValue("@AccountSubType", AcctSubType);
        cmd.Parameters.AddWithValue("@CheckingOption", CheckOption);

        conn.Open();
        cmd.ExecuteNonQuery();

        conn.Close();
    }
}

}

【问题讨论】:

    标签: c# stored-procedures ado.net sqlcommand


    【解决方案1】:
    using System.Data;
    

    您需要引用System.Data。请参阅MSDN Reference for the CommandType Enumeration。直接引用:

    命名空间:System.Data
    Assembly:System.Data(在 System.Data.dll 中

    【讨论】:

      【解决方案2】:

      我还为您的SqlConnectionSqlCommand 对象推荐其他 using 语句。由于它们都实现了IDisposable 接口,因此您可以执行以下操作:

      string ConnString = ConfigurationManager.AppSettings["AssociatedBank2011ConnectionString"];
      using (SqlConnection conn = new SqlConnection(ConnString))
      using (SqlCommand cmd = new SqlCommand("uspInsertPersonalAccountApplcation", conn))
      {
          cmd.Commandtype = CommandType.StoreProcedure;
          cmd.Parameters.AddWithValue("@AccountType", AcctType);
          cmd.Parameters.AddWithValue("@AccountSubType", AcctSubType);
          cmd.Parameters.AddWithValue("@CheckingOption", CheckOption);
      
          conn.Open();
          cmd.ExecuteNonQuery();
      }
      

      这样,如果您的代码正常运行在 using 块中引发异常,您的 SqlConnectionSqlCommand 将自行清理。

      【讨论】:

        【解决方案3】:

        在这种情况下,您可以按 CTRL + 。 (ctrl + dot) 来获得一个建议,比如你想使用 System.Data 添加...

        附:教人钓鱼……

        【讨论】:

          猜你喜欢
          • 2023-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多