使用存储过程和ADO.NET更新数据库
本文关键字:NET 更新 数据库 ADO 存储过程 | 更新日期: 2023-09-27 18:25:46
我想我的类中缺少一个"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();
}
}
}
using System.Data;
您需要参考System.Data
。请参阅MSDN参考以了解CommandType
枚举。直接报价:
命名空间:System.Data
程序集:System.Data(System.Data.dll中的)
我还建议您为SqlConnection
和SqlCommand
对象使用其他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块中抛出异常,则SqlConnection
和SqlCommand
将自行清理。
在这种情况下,您可以按CTRL+。(ctrl+dot)来获得一个建议,比如你想使用System.Data…添加吗
附言:教男人钓鱼。。。