- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在构建一个程序,该程序将用户之间的消息存储在数据库中,并在按下下面的按钮时将这些消息返回给用户。我正在使用一个使用 OleDbConnection
和 DataReader
的 SQL CE 数据库。
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
类,但是当我在 Lecturer
类中重复使用代码并进行微小更改时,OledbDataReader
说 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]);
}
}
最佳答案
使用反射器:
OleDbCommand.ExcuteReader
:
public OleDbDataReader ExecuteReader(CommandBehavior behavior)
{
OleDbDataReader reader;
IntPtr ptr;
OleDbConnection.ExecutePermission.Demand();
Bid.ScopeEnter(out ptr, "<oledb.OleDbCommand.ExecuteReader|API> %d#, behavior=%d{ds.CommandBehavior}\n", this.ObjectID, (int) behavior);
try
{
this._executeQuery = true;
reader = this.ExecuteReaderInternal(behavior, "ExecuteReader");
}
finally
{
Bid.ScopeLeave(ref ptr);
}
return reader;
}
CommandBehavior 是 default.this.ExecuteReaderInternal() 返回的读取器---->
private OleDbDataReader ExecuteReaderInternal(CommandBehavior behavior, string method)
{
OleDbDataReader dataReader = null;
OleDbException previous = null;
int num2 = 0;
try
{
object obj2;
int num;
this.ValidateConnectionAndTransaction(method);
if ((CommandBehavior.SingleRow & behavior) != CommandBehavior.Default) behavior |= CommandBehavior.SingleResult;
switch (this.CommandType)
{
case ((CommandType) 0):
case CommandType.Text:
case CommandType.StoredProcedure:
num = this.ExecuteCommand(behavior, out obj2);
break;
case CommandType.TableDirect:
num = this.ExecuteTableDirect(behavior, out obj2);
break;
default:
throw ADP.InvalidCommandType(this.CommandType);
}
if (this._executeQuery)
{
try
{
dataReader = new OleDbDataReader(this._connection, this, 0, this.commandBehavior);
switch (num)
{
case 0:
dataReader.InitializeIMultipleResults(obj2);
dataReader.NextResult();
break;
case 1:
dataReader.InitializeIRowset(obj2, ChapterHandle.DB_NULL_HCHAPTER, this._recordsAffected);
dataReader.BuildMetaInfo();
dataReader.HasRowsRead();
break;
case 2:
dataReader.InitializeIRow(obj2, this._recordsAffected);
dataReader.BuildMetaInfo();
break;
case 3:
if (!this._isPrepared) this.PrepareCommandText(2);
OleDbDataReader.GenerateSchemaTable(dataReader, this._icommandText, behavior);
break;
}
obj2 = null;
this._hasDataReader = true;
this._connection.AddWeakReference(dataReader, 2);
num2 = 1;
return dataReader;
}
finally
{
if (1 != num2)
{
this.canceling = true;
if (dataReader != null)
{
dataReader.Dispose();
dataReader = null;
}
}
}
}
try
{
if (num == 0)
{
UnsafeNativeMethods.IMultipleResults imultipleResults = (UnsafeNativeMethods.IMultipleResults) obj2;
previous = OleDbDataReader.NextResults(imultipleResults, this._connection, this, out this._recordsAffected);
}
}
finally
{
try
{
if (obj2 != null)
{
Marshal.ReleaseComObject(obj2);
obj2 = null;
}
this.CloseFromDataReader(this.ParameterBindings);
}
catch (Exception exception3)
{
if (!ADP.IsCatchableExceptionType(exception3)) throw;
if (previous == null) throw;
previous = new OleDbException(previous, exception3);
}
}
}
finally
{
try
{
if (dataReader == null && 1 != num2) this.ParameterCleanup();
}
catch (Exception exception2)
{
if (!ADP.IsCatchableExceptionType(exception2)) throw;
if (previous == null) throw;
previous = new OleDbException(previous, exception2);
}
if (previous != null) throw previous;
}
return dataReader;
}
this._executeQuery
包装了 OleDbDataReader
的新实例,如果它不运行 dataReader
将为 null
.
读取器作为 null 返回的唯一方法是内部 RunExecuteReader 方法为 returnStream 传递“false”,但事实并非如此。
这里是唯一将this._executeQuery
设置为false
的地方,但是这个因为Bid.ScopeEnter
没有被并行调用> 和 Bid.ScopeLeave
。
public override int ExecuteNonQuery()
{
int num;
IntPtr ptr;
OleDbConnection.ExecutePermission.Demand();
Bid.ScopeEnter(out ptr, "<oledb.OleDbCommand.ExecuteNonQuery|API> %d#\n", this.ObjectID);
try
{
this._executeQuery = false;
this.ExecuteReaderInternal(CommandBehavior.Default, "ExecuteNonQuery");
num = ADP.IntPtrToInt32(this._recordsAffected);
}
finally
{
Bid.ScopeLeave(ref ptr);
}
return num;
}
理论上,如果无法执行查询,数据读取器可以为空。
关于c# - OleDbDataReader = 空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22369292/
我有以下代码: OleDbDataReader xlsReader = new OleDbCommand("Select * from [" +spreadSheetName +
我目前正在构建一个程序,该程序将用户之间的消息存储在数据库中,并在按下下面的按钮时将这些消息返回给用户。我正在使用一个使用 OleDbConnection 和 DataReader 的 SQL CE
正在尝试从*.XLS 文件 中读取数据。我能够读取所有单元格字符串,但无法读取单元格中的数字。我尝试使用 .ToString() 方法但没有成功。取回数据库 null。 代码: public xlsC
我做了一个程序,它读取数据库中的数据 我使用 OleDbDataReader 但问题是我有不同的表,这个代码工作得很好,但我发现它有点“硬编码”或 recursive 这是我的示例代码
读取 xlsx 文件时遇到一个非常奇怪的问题(我正在使用 OleDbDataReader)。 我有一列包含以下数据: 50595855 59528522 C_213154 23141411 问题是,当
我的 OleDb 访问数据库连接正确地计算了数据库表中的行(条目)数,但是当涉及到使用 for 循环读取其中一个字段的每个实例时,我得到的错误是在获取表中第一个条目的用户名 (teacher_id/s
为了重构我的一些代码,我添加了一个函数来设置 OleDbDataReader . 由于某种奇怪的原因,我得到的返回值是 System.Data.Common.DbDataRecord . 就在函数返回
在它下面是我用来连接到 Access 数据库并从查询中提取值的代码。问题是..我无法从读者对象中取回任何值。我可以看到行数正确,但是我不断收到 InvalidOperationException(无论
我想从 C# 中的 OleDbDataReader 获取检索到的记录数? strQuery = "SELECT * FROM Table_Name" ; d
使用 DataReader 连接到数据库后,如何计算行数? 谢谢。 最佳答案 数据读取器只是前向的,所以他们在第一次填充时没有计数。你可以做几件事来解决这个问题: 运行一个单独的命令来获取计数或使用
这是我的代码: OleDbConnection connection = new OleDbConnection( "Provider=Microsoft.ACE.OLEDB.12.0;Data
我有一个 Excel 表,显示部门明智的员工工资详细信息。只是我需要将这些详细信息保存到 MySql 中。 当我使用 oledbdatareader 时,它从第一行开始读取。但我必须从不同的行中进行选
我正在尝试使用 MS 访问数据库更改密码选项.... 请大家帮帮我.... 这里是代码:默认.aspx.cs protected void Button1_Click(object sender, E
我一直在处理一个项目(使用 Visual Studio 2013),我需要从本地存储的 MS Access 2007 数据库中检索信息。我现在正在使用 OleDb 来处理连接。数据库有一个表,有几个字
我是一名优秀的程序员,十分优秀!