c#中使用access数据库,OleDbDataReader reader =comm.ExecuteRead

.Net技术 码拜 7年前 (2017-04-22) 1624次浏览
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Access_Project
{
public partial class finicaldetail : Form
{
private int current = 1;
string connstr = “Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\datebase\Financial.mdb;”;
public finicaldetail()
{
InitializeComponent();
current = 1;
ShowCondition();  //假如第一个学生存在,则显示
}
private void ShowCondition()
{
string sql = String.Format(“SELECT * FROM FinancialAffairs WHERE ID=”{0}””, current);
using (OleDbConnection db = new OleDbConnection(connstr))
{
db.Open();
OleDbCommand comm = new OleDbCommand(sql, db);
OleDbDataReader reader =comm.ExecuteReader();  //这里异常 信息:标准表达式中数据类型不匹配。
if (reader.Read())
{
txtName.Text = reader.GetString(1);
string incomeandpay = reader.GetString(2);
if (incomeandpay == “收”)
income.Checked = true;
else
pay.Checked = true;
dtday.Value = reader.GetDateTime(3);
cboDept.Text = reader.GetString(4);
money.Text = reader.GetString(5);
}
else
{
MessageBox.Show(“前面或后面已无数据了”, “没有数据”, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
reader.Close();
}
}
解决方案

10

ID字段在数据库里可能是整形或其他非字符串格式
而你的SQL语句里 ID是字符串(CHAR VARCHAR)

10

语句有问题,你看看字段数据类型与你的语句一致不

10

WHERE ID=”{0}” -> WHERE ID={0}  去掉单引号

10

ID=”{0}”
id估计你是int,然后你传做string了

10

到的结果集里面第二列是什么类型的?

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c#中使用access数据库,OleDbDataReader reader =comm.ExecuteRead
喜欢 (0)
[1034331897@qq.com]
分享 (0)