string connStr = "server=kofo;database=pubs;uid=sa;pwd=kofo123";
SqlConnection myConnection = new SqlConnection(connStr);
try
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand("TestRetValue",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter myParam = myCommand.Parameters.Add(new SqlParameter("@RETURN_VALUE",SqlDbType.Int,4));
myParam.Direction = ParameterDirection.ReturnValue;
myCommand.ExecuteNonQuery();
int me = (int)myParam.Value;
MessageBox.Show(me.ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
myConnection.Close();
}
本文提供了一个使用C#连接SQL Server数据库并调用存储过程来获取返回值的示例。通过创建SqlConnection对象并设置连接字符串,然后使用SqlCommand对象指定存储过程名称及参数,最后执行存储过程并显示返回值。
194

被折叠的 条评论
为什么被折叠?



