由于C#控制台应用程序中对象的当前状态,操作无效
我试图为控制台应用程序编写简单的程序,但每次都会遇到这个错误。由于C#控制台应用程序中对象的当前状态,操作无效,c#,asp.net,C#,Asp.net,我试图为控制台应用程序编写简单的程序,但每次都会遇到这个错误。 几个月前,这段代码运行正常,但现在它给了我这个错误, 我重新安装了visual studio和oracle数据库,但也出现了相同的错误… 请帮忙 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Oracle.ManagedDataAccess.Client; using Oracle.Managed
几个月前,这段代码运行正常,但现在它给了我这个错误,
我重新安装了visual studio和oracle数据库,但也出现了相同的错误…
请帮忙
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
namespace dataTestDemo
{
class Program
{
static void Main(string[] args)
{
string constr;
constr = "User Id = hr; Password = tiger; data source = localhost: 1521/ORCL; pooling = false";
try
{
OracleConnection con = new OracleConnection();
con.ConnectionString = constr;
con.open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = "select salary from employees where employee_id = 109";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())// Here, I am getting this error..
{
Console.WriteLine("Employee Salary = " + reader.GetString(0));
}
con.close();
Console.WriteLine();
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException);
Console.WriteLine(ex.Data);
Console.ReadLine();
}
}
}
}
错误:由于对象的当前状态,操作无效。
System.Collections.ListDictionaryInternal
在代码中,您创建了一个名为con的对象,该对象属于OracleConnection类型。但是在设置ConnectionString属性之后,您永远不会使用此对象。我猜您需要打开连接,并以某种方式将cmd对象链接到此连接。我看不出您在命令对象上设置连接的位置,您也应该打开连接
OracleConnection con = new OracleConnection();
con.ConnectionString = constr;
// Open the connection
con.Open();
OracleCommand cmd = new OracleCommand();
// Set the connection on the command object
cmd.Connection = connection;
cmd.CommandText = "select salary from employees where employee_id = 109";
OracleDataReader reader = cmd.ExecuteReader();
// Don't forget to close your connection at some point
我故意忽略了错误检查,并确保连接在使用后关闭。下面链接的底部有一个例子,可以进一步帮助您
希望这对你有帮助,祝你好运 谢谢你,这起作用了……:)