Sql server 存储过程无法';联系不到
无法执行存储过程。谁能告诉我并指出我愚蠢的错误 我收到的错误消息是 无效操作。连接已关闭 代码:Sql server 存储过程无法';联系不到,sql-server,tsql,stored-procedures,Sql Server,Tsql,Stored Procedures,无法执行存储过程。谁能告诉我并指出我愚蠢的错误 我收到的错误消息是 无效操作。连接已关闭 代码: 要调用.BeginTransaction(),您的连接需要已经打开-因此将代码更改为: using (SqlConnection connection = new SqlConnection(@"Data Source=19NNZP;Initial Catalog=ivr;Persist Security Info=True;User ID=sa;Password=sa")) { /
要调用
.BeginTransaction()
,您的连接需要已经打开-因此将代码更改为:
using (SqlConnection connection = new SqlConnection(@"Data Source=19NNZP;Initial Catalog=ivr;Persist Security Info=True;User ID=sa;Password=sa"))
{
// set up the SqlCommand
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.fax_UpdateFaxReply";
// SqlDbType should be *NVarChar* to exactly match the stored procedure parameter's type!
// Otherwise you'll have an implicit conversion happening....
command.Parameters.Add("@uid", SqlDbType.NVarChar, 50).Value = RPBAL.UlyssesID ;
SqlTransaction transaction;
try
{
// open connection, start transaction
connection.Open();
transaction = connection.BeginTransaction("SampleTransaction");
// assign transaction to SqlCommand and execute it
command.Transaction = transaction;
command.ExecuteNonQuery();
// if successful - commit the transaction!
transaction.Commit();
connection.Close();
Console.WriteLine("OK");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
throw new Exception(ex.Message);
}
}
}
更改之后,希望此代码可以正常工作。是否尝试使用相同的登录名和参数直接运行SP?
@ulyssesId
参数是什么类型?是否有其他参数(特别是必需的参数)?是的,使用相同的登录凭据直接运行sp没有问题。只有一个参数,它是nvarchar(50)。这是我收到的错误消息“操作无效。连接已关闭。”
using (SqlConnection connection = new SqlConnection(@"Data Source=19NNZP;Initial Catalog=ivr;Persist Security Info=True;User ID=sa;Password=sa"))
{
// set up the SqlCommand
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.fax_UpdateFaxReply";
// SqlDbType should be *NVarChar* to exactly match the stored procedure parameter's type!
// Otherwise you'll have an implicit conversion happening....
command.Parameters.Add("@uid", SqlDbType.NVarChar, 50).Value = RPBAL.UlyssesID ;
SqlTransaction transaction;
try
{
// open connection, start transaction
connection.Open();
transaction = connection.BeginTransaction("SampleTransaction");
// assign transaction to SqlCommand and execute it
command.Transaction = transaction;
command.ExecuteNonQuery();
// if successful - commit the transaction!
transaction.Commit();
connection.Close();
Console.WriteLine("OK");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
throw new Exception(ex.Message);
}
}
}