ado.net里面如何使用存储过程

when 2002-04-03 09:22:13
是不是asp.net里面使用存储过程的效果和sqlcommand一样
...全文
52 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
badtank 2002-04-19
  • 打赏
  • 举报
回复
SqlConnection mConn=new SqlConnection(connectionstring);
string strSQL="storename param1,param2..."
SqlDataAdapter myAdap=new SqlDataAdapter(strSQL,mConn);
DataSet mySet=new DataSet();
myAdap.fill(mySet);
......
karma 2002-04-03
  • 打赏
  • 举报
回复
yes, for example (from QuickStart tutorial):


SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;Trusted_Connection=yes;database=northwind");

// Create stored procedure with out parameter
try
{
SqlCommand CreateProcCommand = new SqlCommand("CREATE PROCEDURE GetCompanyName @CustomerId nchar(5), @CompanyName nchar(40) out as select @CompanyName = CompanyName from Customers where CustomerId = @CustomerId",myConnection);
SqlCommand DropProcCommand = new SqlCommand("IF EXISTS (SELECT name FROM sysobjects WHERE name = 'GetCompanyName' AND type = 'P') DROP PROCEDURE GetCompanyName", myConnection);

myConnection.Open();
DropProcCommand.ExecuteNonQuery(); // remove procedure if it exists
CreateProcCommand.ExecuteNonQuery(); // create procedure

SqlCommand myCommand = new SqlCommand("GetCompanyName", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;

// Fill the parameters collection based upon stored procedure.
SqlParameter workParam = null;

workParam = myCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5);
// ParameterDirection.Input is the default for the Direction property. Thus the following line is not
// needed here. To set the Direction property to its default value, use the following line.
// workParam.Direction = ParameterDirection.Input;

workParam = myCommand.Parameters.Add("@CompanyName", SqlDbType.NChar, 40);
workParam.Direction = ParameterDirection.Output;

myCommand.Parameters["@CustomerID"].Value = "ALFKI";

myCommand.ExecuteNonQuery();
MsgString = "CompanyName = " + myCommand.Parameters["@CompanyName"].Value.ToString();
}
catch(Exception e)
{
MsgString = e.ToString();
}
finally
{
myConnection.Close();
}

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧