简体   繁体   English

使用c#执行SQL查询时出现问题

[英]Trouble in executing the SQL query using c#

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);
            }

        }
    }
}

Please help me to resolve this issue.Thanks. 请帮我解决这个问题。谢谢。 I want to execute SQL query using c# and get the results on console.I guess there is some problem with my code.Please review and let me know the inputs. 我想使用c#执行SQL查询并在控制台上获取结果。我猜我的代码有问题。请查看并告诉我输入。

If you want to print the query data, you should run a command like this: 如果要打印查询数据,则应运行如下命令:

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

..inside the while loop. ..在while循环中。

It appears that you're trying to use the SQL Native Client (normally for use for connecting to Microsoft SQL Server) rather than using an Oracle client. 您似乎正在尝试使用SQL Native Client(通常用于连接到Microsoft SQL Server)而不是使用Oracle客户端。 Whilst the System.Data.OracleClient namespace does exist, it is deprecated . 虽然System.Data.OracleClient命名空间确实存在, 但不推荐使用它 Instead, you may want to consider connecting using an OleDbConnection with the appropriate connection string, so something like: 相反,您可能需要考虑使用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM