我得到一個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注入)... –