怎样取的存储过程中的传出值,怎样在事务中一起执行INSERT语句和存储过程,在线等到11:30

ChristianBoris 2003-10-27 10:05:37
在线等到11:30,各位救急啊!!!!!!!!!!
...全文
31 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChristianBoris 2003-10-27
  • 打赏
  • 举报
回复
谢谢,揭贴,以后多联系啊!:)
saucer 2003-10-27
  • 打赏
  • 举报
回复
some pseudo code:


SqlConnection myConnection = new SqlConnection("Data Source=localhost;User ID=sa;Password=;Initial Catalog=northwind");
myConnection.Open();

SqlCommand myCommand = myConnection.CreateCommand();
SqlTransaction myTrans;
myTrans = myConnection.BeginTransaction();
myCommand.Connection = myConnection;
myCommand.Transaction = myTrans;

try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
myCommand.ExecuteNonQuery();

myCommand.CommandText = "SomeSP";
myCommand.CommandType = CommandType.StoredProcedure;

SqlParameter myParm = myCommand.Parameters.Add("@RowCount", SqlDbType.Int);
myParm.Direction = ParameterDirection.ReturnValue;

myCommand.Parameters.Add("@CategoryName", SqlDbType.NChar, 15);
myCommandParameters["@CategoryName"].Value = "New Category Name";

SqlParameter myOutputParam = new SqlParameter("@NewCategoryID", SqlDbType.Int);
myOutputParam.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(myOutputParam);


myCommand.ExecuteNonQuery();

Int32 rowCount = (Int32)myCommand.Parameters["@RowCount"].Value;
Int32 newID = (Int32)myCommand.Parameters["@NewCategoryID"].Value;


myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}

Console.WriteLine("An exception of type " + e.GetType() +
" was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}
ChristianBoris 2003-10-27
  • 打赏
  • 举报
回复
那怎么把INSERT语句和存储过程一起加入事务当中执行呢?定义两个
oleDbCommoned 分别执行吗?求你帮帮我,给个事务执行的实例来学习
学习,谢谢,谢谢!
saucer 2003-10-27
  • 打赏
  • 举报
回复
OleDbCommand sampleCMD = new OleDbCommand("SampleProc", nwindConn);
sampleCMD.CommandType = CommandType.StoredProcedure;

OleDbParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", OleDbType.Integer);
sampParm.Direction = ParameterDirection.ReturnValue;

sampParm = sampleCMD.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
sampParm.Value = "Sample Value";

sampParm = sampleCMD.Parameters.Add("@OutputParm", OleDbType.VarChar, 28);
sampParm.Direction = ParameterDirection.Output;

nwindConn.Open();

OleDbDataReader sampReader = sampleCMD.ExecuteReader();

Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1));

while (sampReader.Read())
{
Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1));
}

sampReader.Close();
nwindConn.Close();

Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value);
Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);
ChristianBoris 2003-10-27
  • 打赏
  • 举报
回复
sampParm.Direction = ParameterDirection.Output;
上面是不是输出值,但是怎么取出来付值呢?先谢谢思归大哥!
saucer 2003-10-27
  • 打赏
  • 举报
回复
use an output parameter, see

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingstoredprocedureswithcommand.asp

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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