使用c#执行SQL查询的问题
本文关键字:问题 查询 SQL 执行 使用 | 更新日期: 2023-09-27 18:18:56
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查询,并在控制台上获得结果。我猜我的代码有问题。
如果您想要打印查询数据,您应该运行这样的命令:
Console.WriteLine(thisReader["ROW_ADDED_OPRID"].ToString());
. .在while循环中
似乎你正在尝试使用SQL本地客户端(通常用于连接到Microsoft SQL Server),而不是使用Oracle客户端。虽然System.Data.OracleClient
名称空间确实存在,但不推荐使用。相反,您可能想要考虑使用带有适当连接字符串的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);
}