我不确定如何纠正打开DataReader的错误,如果我将其关闭,则会抛出错误。我试图更改命令名称以及其他名称来防止此问题,但是它似乎是持久的。那是第一个问题,第二个问题是我无法从列表中提取正确的数据。当我将信息从数据库传递到列表时,它应该位于FirstName到First Name列表的列中,然后是Middle Initials等等。我觉得这可能与命令字符串q有关。
我已经尝试了多种不同的方法,但是似乎都没有用。我更改了读者名称,尝试根据需要关闭和打开。至于我试图更改命令字符串q的提取数据,我试图以不同的方式传递信息。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Windows.Forms;
namespace DEITO
{
/// <summary>
/// Interaction logic for Customers.xaml
/// </summary>
public partial class Customers : Window
{
public static List<String> CustomerFirstName = new List<string>();
List<String> CustomerMiddleInitial = new List<string>();
List<String> CustomerLastName = new List<string>();
List<String> CustomerTaxClass = new List<string>();
List<String> CustomerEmail = new List<string>();
public Customers()
{
InitializeComponent();
RetriveCustomerInfo();
}
public static string name;
private List<String> RetriveCustomerInfo()
{
DataTable dt = new DataTable();
try
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\SCook\Documents\LoginInformation.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
string q = "SELECT * FROM CustomerInfo";
SqlCommand Comm = new SqlCommand(q, con);
// SqlDataAdapter DAdapter = new SqlDataAdapter();
SqlDataReader ReadThis = Comm.ExecuteReader();
int Readerint = 0;
while (ReadThis.Read())
{
// int StringCount = 0;
name = ReadThis.GetString(1);
//string MN = Reader.GetString(StringCount);
//string LN = Reader.GetString(StringCount);
//string TC = Reader.GetString(StringCount);
//string EM = Reader.GetString(StringCount);
CustomerFirstName.Add(name);
//CustomerMiddleInitial.Add(MN);
//CustomerLastName.Add(LN);
//CustomerTaxClass.Add(TC);
//CustomerEmail.Add(EM);
// cmd..Parameters.Add(new SqlParameter("@FMT", name);
Comm.ExecuteNonQuery();
Comm.Parameters.Clear();
con.Close();
Readerint = Readerint + 1;
}
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
/*string output = string.Join("\n", CustomerFirstName);
string Out1 = string.Join("\n", CustomerMiddleInitial);
string Out2 = string.Join("\n", CustomerLastName);
string Out3 = string.Join("\n", CustomerTaxClass);
string Out4 = string.Join("\n", CustomerEmail);
System.Windows.Forms.MessageBox.Show(output); */
return CustomerFirstName;
}
private void Exit_Clicked(object sender, RoutedEventArgs e)
{
this.Close();
}
public class CustomerInformation
{
public string FirstName { get; set; }
public string MiddleInital { get; set; }
public string LastName { get; set; }
public string TaxClass { get; set; }
public string Email { get; set; }
}
public void FillDataGridWithCustInfo()
{
int ArrayNum = 0;
Int32 length = CustomerFirstName.Count;
while (length >= ArrayNum)
{
CustomerInformation TempCust = new CustomerInformation();
TempCust.FirstName = name;
CustomerDataGrid.Items.Add(TempCust);
ArrayNum = ArrayNum + 1;
}
}
private void AddnewCustomer_Click(object sender, RoutedEventArgs e)
{
CustomerInformation TempCust = new CustomerInformation();
TempCust.FirstName = FirstNameText.Text;
TempCust.MiddleInital = MiddleInitalText.Text;
TempCust.LastName = LastNameText.Text;
TempCust.TaxClass = TaxClassText.Text;
TempCust.Email = EmailText.Text;
CustomerDataGrid.Items.Add(TempCust);
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\SCook\Documents\LoginInformation.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
string q = "insert into CustomerInfo (FirstName, MiddleInital, LastName, TaxClass, Email) Values(@FMT, @MIT, @LNT, @TCT, @EMT)";
SqlCommand Comm = new SqlCommand(q, con);
Comm.Parameters.AddWithValue("@FMT", FirstNameText.Text);
Comm.Parameters.AddWithValue("@MIT", MiddleInitalText.Text);
Comm.Parameters.AddWithValue("@LNT", LastNameText.Text);
Comm.Parameters.AddWithValue("@TCT", TaxClassText.Text);
Comm.Parameters.AddWithValue("@EMT", EmailText.Text);
Comm.ExecuteNonQuery();
Comm.Parameters.Clear();
con.Close();
System.Windows.Forms.MessageBox.Show("You have successfully added to the database");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//RetriveCustomerInfo();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
FillDataGridWithCustInfo();
}
}
}
我正在尝试从数据库CustomerInfo中获取数据并将其填充到CustomerDataGrid中。似乎它应该比我想像的要复杂,但是SQL不是我最喜欢处理的语言。当我将.getstring(some Number)从0更改为1时,它只是从第一行数据中提取。因此,与其给我下一个人的名字,不如给我中间的名字首字母。
异常消息:已经有一个与此命令相关联的打开的数据读取器,必须首先将其关闭。