나는 현재 데이터베이스에 사용자간에 메시지를 저장하고 아래 버튼을 누르면됩니다 때 사용자에게 이러한 메시지를 반환하는 프로그램을 짓고 있어요. OleDbConnection
을 사용하고 DataReader
을 사용하는 SQL CE 데이터베이스를 사용하고 있습니다.방법 OleDbDataReader = null이
private void button3_Click(object sender, EventArgs e)
{
string [] lec_name = new string [10] ;
string [] content = new string [10] ;
string conn = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=C:\\Users\\Leon\\Admin.sdf";
OleDbConnection connection = new OleDbConnection(conn);
OleDbCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM Contact_DB WHERE Student_ID =" + iD + " AND Direction = '" + "To the student" + "'";
try
{
connection.Open();
}
catch (Exception ex)
{
MessageBox.Show("" + ex.Message);
}
OleDbDataReader reader = command.ExecuteReader();
int up = 0;
int count = 0;
while (reader.Read())
{
lec_name[up] = reader["Lecturer_Name"].ToString();
content[up] = reader["Description"].ToString();
up++;
MessageBox.Show("The lecturer " + lec_name[count] + " has messaged you saying :" + "\n" + content[count]);
count++;
}
}
이 코드는 내 Student
클래스 작동하지만 나는 OledbDataReader
가 null 말한다 Lecturer
클래스 내에서 작은 변화와 코드를 재사용 할 때, 사람이 이유를 알고?
Btw 반환되는 값이 null이 아니므로 판독기 자체가 null입니다. 다음은 작동하지 않는 코드입니다.
private void button2_Click(object sender, EventArgs e)
{
string [] studentID = new string [10] ;
string [] content = new string [10] ;
string conn = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=C:\\Users\\Leon\\Admin.sdf";
OleDbConnection connection = new OleDbConnection(conn);
OleDbCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM Contact_DB WHERE Lecturer_Name =" + full + " AND Direction = '" + "To the lecturer" + "'";
try
{
connection.Open();
}
catch (Exception ex)
{
MessageBox.Show("" + ex.Message);
}
OleDbDataReader reader1 = command.ExecuteReader();
int up = 0;
int count = 0;
while (reader1.Read())
{
studentID[up] = reader1["Student_ID"].ToString();
content[up] = reader1["Description"].ToString();
up++;
}
MessageBox.Show("The student " + studentID[count] + " has messaged you saying :" + "\n" +content[count]);
}
}
예외가 있습니까? – Szymon
게시 된 코드가 작동 코드입니까 아니면 작동하지 않는 코드입니까? 몇 가지 관찰 : 1. SQL 인젝션을 방지하기 위해 매개 변수화 된 쿼리를 배웁니다. 2. 연결을 열려고 시도하는 경우 예외가 발생하면 다른 모든 방법도 실패하므로 메서드를 종료해야합니다. 3.'using' 블록을 사용하거나 독자에게'Close()'를 호출하십시오. – Tim
@Szymon yeah (토큰 라인 번호, Token line offset, Token in error ,,]) while (reader.Read()) 라인에서 중단 점을 사용할 때 datareader를 발견했습니다. null –