具有C#错误的SQL结果 [英] SQL Results with C# error

查看:90
本文介绍了具有C#错误的SQL结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试从SQL表到C#控制台视图显示一组结果.我首先尝试仅显示其中一个查询,但在第36行却失败了.
我只用C#和SQL编写了一个星期左右的代码,所以确实有些帮助了,这个站点已经提供了很多帮助.
我不得不删除代码的一些细节,但否则如下所示.


Hi
I am trying to display a set of results from a SQL table to a C# console view. I have started by trying to display just one of the queries but I get a fail on line 36.
I have only been coding for a week or so with C# and SQL so some help would realy go down well and this site has already helped alot already.
I have had to remove some of the details of my code but it is as below otherwise.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace SQL_results
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = null;
            conn = new SqlConnection("Data Source=**********;" +
                                        "Initial Catalog=******; +
                                        "Persist Security Info=True;" +
                                        "User ID=******;" +
                                        "Password=******;" +
                                        "connection timeout=20");
            conn.Open();
            Console.Write("The SQL Database is connected." + "\n\n");
           try
            {
                SqlDataReader myReader = null;
                string strSQLCommand = "SELECT COUNT (*) FROM ****** WHERE loginDisabled = ''FALSE'' OR loginDisabled IS NULL";
                SqlCommand command = new SqlCommand(strSQLCommand, conn);
                SqlDataReader result = command.ExecuteReader();
                while (myReader.Read())
                {
                    Console.WriteLine(myReader["Total Vault Active Accounts:" + result].ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.ReadLine();
            }
        }
    }
}



我得到的错误如下:

SQL数据库已连接.
System.NullReferenceException:对象引用未设置为obj
的实例 等
在D:\ USERDATA \ RowellCJ \ My Documen中的SQL_results.Program.Main(String [] args)中
ts \ Visual Studio 2010 \ Projects \ SQL结果\ SQL结果\ Program.cs:第36行


然后,我可以显示第一个查询.



Error I get is as below:

The SQL Database is connected.
System.NullReferenceException: Object reference not set to an instance of an obj
ect.
at SQL_results.Program.Main(String[] args) in D:\USERDATA\RowellCJ\My Documen
ts\Visual Studio 2010\Projects\SQL results\SQL results\Program.cs:line 36


Then after I can get the first query to display.
How would I get it to run through 12 different queries and display them all in the the console window in a list for?

推荐答案

好吧,如果只是首先,做C#并且SQL太疯狂了,您应该在将C#核心与SQL一起使用之前以及学习SQL之前,先学习它.但是,如今没有人希望成为一名真正的程序员....

您的代码没有行号,但是我可以猜测您的错误是什么. myReader设置为null,从不设置任何其他值.我知道您正在学习,但是当收到错误消息时,有时您所要做的就是假设该消息为真,然后扫描代码以查看是否有任何理由相信该变量会不为null,或者至少在可能为null的地方不为null.您还可以设置一个断点并逐步执行,以查看您认为应将其设置为其他位置的地方,并查看是否存在.
Well, if you''re just starting, doing C# AND SQL is insane, you should learn core C# before you use it with SQL, and before you learn SQL. But, no-one wants to be a real programmer nowadays....

Your code doesn''t have line numbers, but I can guess what your error is. myReader is set to null, and never set to any other value. I know you''re learning, but when you get an error message, sometimes all you need to do, is assume that the message is true, and scan the code to see if there''s any reason to believe the variable would ever not be null, or at least, where it might turn out to be null. You can also set a breakpoint and step through to see where you think it should be set to something else, and see if it is.


变量"myReader"为null且从不设置.摆脱结果"变量.不需要.试试这个:

The variable "myReader" is null and is never set. Get rid of the "result" variable. It is not needed. Try this:

myReader = command.ExecuteReader();
while (myReader.Read())
{
   Console.WriteLine("Total Vault Active Accounts: " + myReader.GetInt32(0));
}



对myReader.GetInt32(0)的调用以整数形式返回返回的SQL数据的第一列中的值.我希望这可以解决您的问题.祝你好运!



The call to myReader.GetInt32(0) retrieves the value in the first column of returned SQL data as an integer. I hope this resolves your problem. Good luck!


这篇关于具有C#错误的SQL结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆