您的位置:首页 > 其它

在.NET中调用存储过程

2004-07-23 16:35 302 查看
今天试了一下用存储过程取得数据。归纳方法如下:
1.用SqlCommand和DataSet:
SqlConnection conn=new SqlConnection("server=(local);uid=;password=;database=");
SqlCommand cmd=new SqlCommand("StoreProcedure",connn);
cmd.CommandType=CommandType.StoreProcedure;
SqlDataAdapter dsCommand=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
dsCommand.Fill(ds);
2.
用SqlCommand和SqlDataAdapter
不带参数:
Sqlconnection conn=new SqlConnection("server=(local);uid=;password=;database=");
SqlCommand cmd=new SqlCommand("StoreProcedure",conn);
cmd.CommandType=CommandType.StoreProcedure;
SqlDataReader dr=cmd.ExecuteReader()
while(dr.Read())
{
Response.Write(dr.Item["Field"]);
}
带输入参数:
Sqlconnection conn=new SqlConnection("server=(local);uid=;password=;database=");
SqlCommand cmd=new SqlCommand("StoreProcedure",conn);
cmd.CommandType=CommandType.StoreProcedure;

param=command.Parameters.Add("@inputText",SqlDbType.Int);
param.Value=inputValue;
SqlDataReader dr=cmd.ExecuteReader()
while(dr.Read())
{
}
conn.Close()
带输出参数:
Sqlconnection conn=new SqlConnection("server=(local);uid=;password=;database=");
SqlCommand cmd=new SqlCommand("StoreProcedure",conn);
cmd.CommandType=CommandType.StoreProcedure;

param=command.Parameters.Add("@outText",SqlDbType.Int);
param.Direction=ParameterDirection.Output;
SqlDataReader dr=cmd.ExecuteReader()
while(dr.Read())
{
}
outValue=command.Parameters["@outText"].Value;
conn.Close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: