用vs2005开发存储过程遇到的数据库连接失败的问题

jiangchuandong 2007-01-19 01:12:53
创建过程参考msdn和cnblogs.com的文章步骤
http://www.cnblogs.com/ugoer/archive/2005/04/01/129986.html
(本机未装sqlserver2005,我在sqlexpress上建立的表)

稍微修改了下代码:
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlServer;
using System.Data.SqlTypes;


public partial class StoredProcedures
{
[SqlProcedure]
public static void Hello()
{
SqlContext.GetPipe().Send("hello ,uGoer!");
}

[SqlProcedure]
public static void InsertData(SqlString name)
{
SqlCommand InsertCurrencyCommand = SqlContext.GetCommand(); // 修改部分,原为SqlContext.GetCommand()方法;
InsertCurrencyCommand.CommandText = "INSERT INTO table1 (Name, addDate) VALUES ('"+name.Value+"', '" + DateTime.Now.ToString() + "')";
InsertCurrencyCommand.ExecuteNonQuery();
}

[SqlFunction]
public static SqlString testFunction()
{
return "hello , cnBlogs.com";
}
};

然后部署存储过程,在测试的时候前面的那个执行成功,而后面的方法执行未成功,是什么原因呢(Connection property has not been initialized)
错误:
Auto-attach to process '[472] sqlservr.exe' on machine 'cec-jcd' succeeded.
'sqlservr.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'sqlservr.exe' (Managed): Loaded 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\SqlAccess.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'sqlservr.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'sqlservr.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'sqlservr.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_32\System.Transactions\2.0.0.0__b77a5c561934e089\System.Transactions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'sqlservr.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\System.Security\2.0.0.0__b03f5f7f11d50a3a\System.Security.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'sqlservr.exe' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'sqlservr.exe' (Managed): Loaded 'SqlServerProject1', Symbols loaded.
'CEC-JCD;.Net SqlClient Data Provider;1808' (Managed): Loaded 'C:\WINNT\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', No symbols loaded.
Hello World
A .NET Framework error occurred during execution of user defined routine or aggregate 'InsertData':
System.InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized.
System.InvalidOperationException:
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at StoredProcedures.InsertData(SqlString name)
.
No rows affected.
(0 row(s) returned)
Finished running sp_executesql.
A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll
The thread 'cec-jcd\sqlexpress [53]' (0x3f0) has exited with code 0 (0x0).
The program '[472] [SQL] cec-jcd: cec-jcd\sqlexpress' has exited with code 0 (0x0).
The program '[472] sqlservr.exe: Managed' has exited with code 259 (0x103).
...全文
221 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
vengair 2007-01-19
  • 打赏
  • 举报
回复
那你用这种方式写吧 微软提供的标准参考
我一直没问题 扩展也方便
[SqlProcedure]
public static void sp_Question_GetLibraries()
{
using (SqlConnection myConnection = new SqlConnection("context connection=true"))
{
myConnection.Open();

SqlCommand myCommand = new SqlCommand("SELECT * FROM dbo.[Question.Libraries] ORDER BY [LibraryName]", myConnection);

SqlContext.Pipe.ExecuteAndSend(myCommand);
}
}
jiangchuandong 2007-01-19
  • 打赏
  • 举报
回复
楼上的朋友SqlContext.GetCommand() 方法是没有的,你看看msdn
jxzhang615 2007-01-19
  • 打赏
  • 举报
回复
关注
vengair 2007-01-19
  • 打赏
  • 举报
回复
[SqlProcedure]
public static void InsertData(SqlString name)
{
using (SqlConnection myConnection = new SqlConnection("context connection=true"))
{
myConnection.Open();
SqlCommand InsertCurrencyCommand = SqlContext.GetCommand(); // 修改部分,原为SqlContext.GetCommand()方法;
InsertCurrencyCommand.CommandText = "INSERT INTO table1 (Name, addDate) VALUES ('"+name.Value+"', '" + DateTime.Now.ToString() + "')";
InsertCurrencyCommand.ExecuteNonQuery();
}
}

110,536

社区成员

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

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

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