using System;
using System.Data.SqlClient;
namespace ConsoleCSharp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class DataReader_SQL
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
try
{
SqlConnection thisConnection = new SqlConnection(@"Network Library=dbmssocn;Data
Source=sourcename,1655;database=Oracle;User id=sysadm;Password=password;");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT * FROM SYSADM.PS_RQ_DEFECT_NOTE where ROW_ADDED_OPRID = 'github'";
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine(thisCommand.CommandText);
Console.ReadKey();
}
thisReader.Close();
thisConnection.Close();
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
}
}
}
请帮我解决这个问题。谢谢。我想使用c#执行SQL查询并在控制台上获取结果。我猜我的代码有问题。请查看并告诉我输入。
答案 0 :(得分:2)
如果要打印查询数据,则应运行如下命令:
Console.WriteLine(thisReader["ROW_ADDED_OPRID"].ToString());
..在while循环中。
答案 1 :(得分:0)
您似乎正在尝试使用SQL Native Client(通常用于连接到Microsoft SQL Server)而不是使用Oracle客户端。虽然System.Data.OracleClient
命名空间确实存在,但it is deprecated。相反,您可能需要考虑使用OleDbConnection
与适当的连接字符串进行连接,例如:
using (OleDbConnection con = new OleDbConnection(@"Network Library=dbmssocn;Data Source=sourcename,1655;database=Oracle;User id=sysadm;Password=password;")
{
con.Open();
using (OleDbCommand cmd = con.CreateCommand() )
{
cmd.CommandText = "SELECT * FROM SYSADM.PS_RQ_DEFECT_NOTE where ROW_ADDED_OPRID = 'github'";
using(IDataReader thisReader = cmd.ExecuteReader() )
{
while (thisReader.Read())
{
Console.WriteLine(thisReader["fieldname"]);
Console.ReadKey();
}
}
};
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}