Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未处理的异常:System.IndexOutOfRangeException_C#_.net_Sql Server - Fatal编程技术网

C# 未处理的异常:System.IndexOutOfRangeException

C# 未处理的异常:System.IndexOutOfRangeException,c#,.net,sql-server,C#,.net,Sql Server,我正在尝试将代码连接到SQL Server数据库,并在控制台窗口中查看我的数据,但我无法这样做,因为我遇到了一个异常,该异常显示: 未处理的异常:System.IndexOutOfRangeException:userPassword 我在网上尝试了很多解决方案,但没有一个对我有效。我是SQL Server的初学者,所以可能我遗漏了一些明显的东西 这是我的app.config文件: <configuration> <appSettings> <

我正在尝试将代码连接到SQL Server数据库,并在控制台窗口中查看我的数据,但我无法这样做,因为我遇到了一个异常,该异常显示:

未处理的异常:System.IndexOutOfRangeException:userPassword

我在网上尝试了很多解决方案,但没有一个对我有效。我是SQL Server的初学者,所以可能我遗漏了一些明显的东西

这是我的
app.config
文件:

<configuration>
    <appSettings>
        <add key="provider" value="System.Data.SqlClient"/>
        <add key="connectionString" 
             value="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Tayyab\Documents\myDatabase.mdf;Integrated Security=True;Connect Timeout=30"/>
    </appSettings>

    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

谢谢大家!

您可以选择
User
userPassword

Console.WriteLine($"{dataReader["User"]} " + $"{dataReader["userPassword"]}");

但userPassword表中没有
userPassword
列。userPassword表中的列是User和Password。

尝试将
connectionString
更改为
connectionString
(只有一个
t
)。您好,这与您的问题无关,只是一个建议:名称空间应该是有用的,而不是一个日期。使用一个好的名称可能会让您受益。。。毕竟这是一个库:)感谢@stickybit的帮助,因为这个小小的更改已经删除了上述异常,但还有另一个OneHanks@mad.meesh为您提供建议当前异常显示:未处理的异常:System.IndexAutoFrangeException:userPassword at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(字符串字段名)位于System.Data.SqlClient.SqlDataReader.GetOrdinl(字符串名)位于System.Data.SqlClient.SqlDataReader.get_项目(字符串名)位于C:\Users\Tayyab\source\repos\6-11-2018\6-11-2018\Program.cs中的(字符串[]args)主(字符串[]args)中的第48行
Console.WriteLine($"{dataReader["User"]} " + $"{dataReader["userPassword"]}");