请问我这些错误 [英] hellp me please with these error
本文介绍了请问我这些错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
错误是这样的:ExecuteReader需要一个开放且可用的连接。连接当前状态已关闭。
这些是类
error is this: ExecuteReader requires an open and available Connection. The connections current state is closed.
these are classes
namespace TestingApplication.Classes
{
class DbBroker
{
static string CS = @"data source=C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA;database=testing_app;integrated security=true";
public SqlConnection conn;
public static DbBroker dbInstance;
public static DbBroker GetBroker()
{
if (dbInstance == null)
{
dbInstance = new DbBroker();
dbInstance.Open();
}
return dbInstance;
}
public SqlConnection Conn()
{
return new SqlConnection(CS);
}
public SqlConnection Open()
{
conn = Conn();
conn.Open();
return conn;
}
public void Close()
{
conn.Close();
}
public string[] GetRow(string query)
{
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader rdr = cmd.ExecuteReader();
string[] res = new string[rdr.FieldCount];
rdr.Read();
for (int i = 0; i < rdr.FieldCount; i++)
res[i] = rdr[i].ToString();
rdr.Close();
return res;
}
public List<string[]> GetRows(string query)
{
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader rdr = cmd.ExecuteReader();
string[] row;
List<string[]> res = new List<string[]>();
while (rdr.Read())
{
row = new string[rdr.FieldCount];
for (int i = 0; i < rdr.FieldCount; i++)
row[i] = rdr[i].ToString();
res.Add(row);
}
rdr.Close();
return res;
}
public string GetString(string query)
{
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader rdr = cmd.ExecuteReader();
string res;
rdr.Read();
res = rdr[0].ToString();
rdr.Close();
return res;
}
}
}
namespace TestingApplication
{
/// <summary>
/// Interaction logic for Test.xaml
/// </summary>
public partial class Test : UserControl
{
int TestId = 1;
public Test()
{
InitializeComponent();
Classes.TestClass test = Classes.TestClass.GetById(TestId);
Dictionary<int, Classes.Question>.Enumerator en = test.questions.GetEnumerator();
en.MoveNext();
lblQuestion.Content = en.Current.Value.text;
foreach(Classes.Answer an in en.Current.Value.answers)
{
RadioButton rb = new RadioButton();
rb.Content = an.text;
rb.FontSize = 12;
rb.Margin = new Thickness(0, 0, 0, 5);
stackPanel1.Children.Add(rb);
}
btnBack.Visibility = Visibility.Hidden;
}
}
}
class TestClass
{
public Dictionary<int, Question> questions;
public int id;
public string title;
public string description;
public DateTime creationdate;
public static TestClass GetById(int id)
{
DbBroker db = DbBroker.GetBroker();
string[] testRow = db.GetRow("select * from tests where id = " + id);
TestClass t = new TestClass();
t.title = testRow[1];
t.questions = new Dictionary<int, Question>();
List<string[]> questionsList = db.GetRows("select questions.id, questions.question,answers.id,answers.answer,answers.question,answers.points from questions join questions_tests on questions_tests.question = questions.id join answers on questions.id = answers.question where questions_tests.test = " + id);
Question q;
foreach (string[] q_string_arr in questionsList)
{
int qid = int.Parse(q_string_arr[0]);
if (!t.questions.ContainsKey(qid))
{
q = new Question();
q.text = q_string_arr[1];
t.questions.Add(qid, q);
t.questions[qid].answers = new List<Answer>();
}
Answer a = new Answer();
a.id = int.Parse(q_string_arr[2]);
a.text = q_string_arr[3];
a.points = int.Parse(q_string_arr[4]);
a.correct = (a.points > 0) ? true : false;
t.questions[qid].answers.Add(a);
}
return t;
}
}
<d:DesignHeight="367" d:DesignWidth="572">
<Grid>
<Label FontSize="12" Content="1. Some question" Height="42" HorizontalAlignment="Left" Margin="12,12,0,0" Name="lblQuestion" VerticalAlignment="Top" Width="239" />
<Line Stroke="#bcb4b2" StrokeThickness="2" X1="0" Margin="0,56,0,0" VerticalAlignment="Top" Stretch="Fill" X2="1" Y1="50" Y2="50" />
<StackPanel Height="250" HorizontalAlignment="Left" Margin="12,65,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="548"/>
<Button Content="Next" Height="34" Margin="471,321,12,0" Name="btnNext" VerticalAlignment="Top" />
<Button Content="Back" Height="34" HorizontalAlignment="Left" Margin="12,321,0,0" Name="btnBack" VerticalAlignment="Top" Width="89" />
</Grid>
</UserControl>
推荐答案
你有一个类级别conn
变量,它作为SQL命令的一部分使用:
You have a class levelconn
variable, which you use as part of the SQL command:
SqlCommand cmd = new SqlCommand(query, conn);
但是......你永远不会打电话给你的Open方法给它一个值,或者打开连接...
我自己也不会这样做:我使用块在中直接创建并打开SqlConnection,这样就会关闭并自动处理。我也会对SqlCommand对象做同样的事情!
But...you never call your Open method to give it a value, or open the connection...
And I wouldn't do it like that myself: I'd create and open the SqlConnection directly within a using
block so that is gets Closed and Disposed automatically. And I'd do the same with the SqlCommand objects as well!
这篇关于请问我这些错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文