代码如下
SqlConnection con = new SqlConnection("server=.;database=superet;trusted_connection=true");
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "selling";
SqlParameter[] par =
{
new SqlParameter("@num",this.textBox1.Text),
new SqlParameter("@amount",this.textBox2.Text)
};
foreach (SqlParameter parms in par)
{
cmd.Parameters.Add(parms);
}
cmd.ExecuteNonQuery();
con.Close();
错误说
ExecuteNonQuery: Connection 属性尚未初始化
怎么解决?
SqlConnection con = new SqlConnection("server=.;database=superet;trusted_connection=true");
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "selling";
SqlParameter[] par =
{
new SqlParameter("@num",this.textBox1.Text),
new SqlParameter("@amount",this.textBox2.Text)
};
foreach (SqlParameter parms in par)
{
cmd.Parameters.Add(parms);
}
cmd.ExecuteNonQuery();
con.Close();
错误说
ExecuteNonQuery: Connection 属性尚未初始化
怎么解决?
解决方案 »
- 求SQL select语句一条
- 如何将触发器或存储过程从另一台机器复制到本地sql中呢?(用语句)
- 在线急等!三表间删除关连数据,郁闷中,请问该怎么做?谢谢高手了!
- ◆◇◆ Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired. ◆◇◆
- 还是同步的问题
- 多表多列聚合汇总查询
- 关于SQL Server2000安装时的密码设置问题(在线等待)
- 在SQL里,能否得到服务器的名称和IP?
- 急!急!急!!!
- 这到底是怎么回事?(kurtzjs尤其应该看看)
- access2003导入到sql server 2000
- 求SQL 遍历 where条件
为了突出新方法的优点,首先介绍一下在.NET中调用存储过程的“官方”方法。另外,本文的所有示例程序均工作于SqlServer数据库上,其它情况类似,以后不再一一说明。本文所有例子均采用C#语言。
要在应用程序中访问数据库,一般性的步骤是:首先声明一个数据库连接SqlConnection,然后声明一个数据库命令SqlCommand,用来执行 SQL语句和存储过程。有了这两个对象后,就可以根据自己的需要采用不同的执行方式达到目的。需要补充的是,不要忘记在页面上添加如下的引用语句: using System.Data.SqlClient。 就执行存储过程来说,如果执行的是第一类存储过程,那么就要用一个 DataAdapter将结果填充到一个DataSet中,然后就可以使用数据网格控件将结果呈现在页面上了;如果执行的是第二和第三种存储过程,则不需要此过程,只需要根据特定的返回判定操作是否成功完成即可。(1)执行一个没有参数的存储过程的代码如下:SqlConnection conn=new SqlConnection(“connectionString”);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "NameOfProcedure";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
然后只要选择适当的方式执行此处过程,用于不同的目的即可。 (2)执行一个有参数的存储过程的代码如下(我们可以将调用存储过程的函数声明为ExeProcedure(string inputdate)):SqlConnection conn=new SqlConnection(“connectionString”);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "NameOfProcedure";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
(以上代码相同,以下为要添加的代码)
param = new SqlParameter("@ParameterName", SqlDbType.DateTime);
param.Direction = ParameterDirection.Input;
param.Value = Convert.ToDateTime(inputdate);
da.SelectCommand.Parameters.Add(param);
这样就添加了一个输入参数。若需要添加输出参数:param = new SqlParameter("@ParameterName", SqlDbType.DateTime);
param.Direction = ParameterDirection.Output;
param.Value = Convert.ToDateTime(inputdate);
da.SelectCommand.Parameters.Add(param);
若要获得参储过程的返回值:param = new SqlParameter("@ParameterName", SqlDbType.DateTime);
param.Direction = ParameterDirection.ReturnValue;
param.Value = Convert.ToDateTime(inputdate);
da.SelectCommand.Parameters.Add(param);
从上面的代码我们可以看出,当存储过程比较多或者存储过程的参数比较多时,这种方法会大大影响开发的速度;另外一方面,如果项目比较大,那么这些用于数据库逻辑的函数在以后的维护中也是一个很大的负担。那么,有没有一种改进的方法可以解决这个问题呢?想到在执行没有参数的存储过程时只需要传入一个存储过程的名字就可以调用相应的存储过程,而且在SqlServer数据库中我们可以直接在查询分析器中敲入“存储过程名(参数列表)”样的字符串就可以执行存储过程,那么,是否可以把这种思想应用到应用程序中呢? 于是在编译器中键入相应代码。这些代码是在调用不带参数的存储过程的代码的基础上改的。具体代码如下:SqlConnection conn=new SqlConnection(“connectionString”);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "NameOfProcedure(’para1’,’para2’,para3)";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
为了使代码更具有代表性,要调用的存储过程的第一个和第二个参数都为字符串类型,第三个参数为整型。执行以后发现,完全可以达到预期的效果! 两种调用方法的比较
通过比较我们可以看到,第二种方法具有一个很明显的优点,那就是可以提高开发速度,节省开发时间,而且代码容易维护,在一定程度上也减少了系统大小。但是,由于对存储过程参数的处理比较笼统,如果要获取输出参数或者得到存储过程的返回值,这种方法就不能满足需要了。虽然如此,但是,这种方法毕竟可以让开发人员少些很大一部分的代码。如果不需要获取输出参数和返回值,那么几乎可以做到“一劳永逸”。因此在实际的程序开发中,这种方法还是具有一定的实用价值的。22222222222222222222222222222222222222222222222222存储过程
以下是SQL中两个存储过程: CREATE PROCEDURE dbo.oa_selectalluser
AS
select * from UserInfo
GO
CREATE PROCEDURE dbo.oa_SelectByID
@id int
AS
select * from UserInfo where ID=@id
GO
一个是带参数的存储过程,一个是不带参数的存储过程.下面介绍怎么在VS2005中使用这两个存储过程.
(一).不带参数的存储过程:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//不带参数的存储过程的使用方法
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["oaConnectionString"].ToString());
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds=new DataSet();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "oa_SelectAllUser";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
在页面中添加了一个GridView控件用来绑定执行存储过程得到的结果.
(二).带参数的存储过程:
protected void btn_search_Click(object sender, EventArgs e)
{
//带参数的存储过程的使用方法
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["oaConnectionString"].ToString());
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "oa_SelectByID";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@id", SqlDbType.Int);
param.Direction = ParameterDirection.Input;
param.Value = Convert.ToInt32(txt_value.Text);
da.SelectCommand.Parameters.Add(param);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
同样,在页面中添加了一个GridView控件用来绑定执行存储过程的结果,另外,在页面中还添加了一个textbox控件和一个BUTTON按钮,上面的执行存储过程是放在按钮的onclick事件中的.textbox控件用来接收存储过程的参数. 楼主参考一下
cmd.connection=con
连接建来给cmd用的,执行cmd之前,必须将连接赋给cmd的连接属性
cmd.connection=con
连接建来给cmd用的,执行cmd之前,必须将连接赋给cmd的连接属性 赞成小陈的