0
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 查询并在控制台上获取结果。我猜我的代码有问题。请查看并让我知道输入。

4

2 回答 2

2

如果要打印查询数据,则应运行如下命令:

Console.WriteLine(thisReader["ROW_ADDED_OPRID"].ToString());

..在while循环内。

于 2013-06-19T08:48:24.680 回答
0

您似乎正在尝试使用 SQL Native Client(通常用于连接到 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);
}
于 2013-06-19T11:39:21.590 回答