我得到一个DataReader初始化错误。我知道这已被回答了很多次,但这些情况似乎不符合我的情况。错误消息开始“执行读取器:连接属性尚未初始化”。数据读取器初始化(再次)
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace BookList
{
class Program
{
static void Main(string[] args)
{
SqlConnection dataConnection = new SqlConnection();
dataConnection.ConnectionString = "Integrated Security = true; Initial Catalog = BIBLIOGRAPHY;Data Source = JBSHAPIRO-PC\\SQLEXPRESS";
dataConnection.Open();
Console.Write("Enter author's name: ");
string person = Console.ReadLine();
SqlCommand datacommand = new SqlCommand();
datacommand.CommandText = "SELECT AUTHOR, TITLE, YEAR, KEYWORDS
FROM BOOKS WHERE AUTHOR = ' " + person + " ' ";
Console.WriteLine("About to Execute: {0}\n\n",
datacommand.CommandText);
SqlDataReader dataReader = datacommand.ExecuteReader();
while (dataReader.Read())
{
string author1 = dataReader.GetString(0);
string title1 = dataReader.GetString(1);
int year1 = dataReader.GetInt32(2);
string keywords1 = dataReader.GetString(3);
Console.WriteLine(
"Author: {0}\n, Title: {1}\n, Year: {2}\n; Key Words: {4)\n\n",
author1, title1, year1, keywords1);
dataReader.Close();
}
dataConnection.Close();
}
}
}
代码工作与C#3写一个不同的数据库,但现在我想用C#6 有什么建议?
有趣知道为什么有关格式错误代码,并不太正确的说法问题“的代码工作与C#3写了不同的数据库”有这么多upvotes。我看到[MCVE]问题如何能够确定(和不SQL注入)... –