注册 登录
编程论坛 C# 论坛

C#数据库处理时遇到问题

小孔 发布于 2016-02-25 10:01, 2013 次点击
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 表格处理
{
    public partial class Form1 : Form
    {
        OleDbConnection conn = new OleDbConnection();
        OleDbCommand comm = new OleDbCommand();
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        DataSet ds = new DataSet();
        public Form1()
        {
            InitializeComponent();
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=studentinfo.mdb";
            conn.Open();
            comm.Connection = conn;
             = "select * from studentinfo where '分数'>75";
            adapter.SelectCommand = comm;
            OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ds.Clear();
            adapter.Fill(ds, "good");
            d1.DataSource = ds.Tables["good"];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            adapter.Update(ds, "good");
        }
 
    }
}
注:在adapter.Fill(ds,"good")这儿提示 :标准表达式中数据类型不匹配。
1 回复
#2
qq10235692232016-02-25 10:41
我和教材上对比了一下,综合个人想法给个意见。好久没有用过数据库了,不过当时也没有学多少,还有吗,我觉得编程中少用中文为好。总之,我没看出来有什么错误的地方。
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 表格处理
{
    public partial class Form1 : Form
    {
        //全局变量最好在构造函数里初始化
        OleDbConnection conn;
        OleDbCommand comm;
        OleDbDataAdapter adapter;
        DataSet ds;
        OleDbCommandBuilder builder;
        
        public Form1()
        {
            InitializeComponent();
            //初始化变量
            conn = new OleDbConnection();
            comm = new OleDbCommand();
            adapter = new OleDbDataAdapter();
            ds = new DataSet();
        
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=studentinfo.mdb";
            //conn.Open();  //这个好像不用打开
            comm.Connection = conn;
             = "select * from studentinfo where '分数'>75";
            adapter.SelectCommand = comm;
            //使用OleDbCommandBuilder注意,对应数据库表中要有主键,否则在执行修改和删除时会出错
            builder = new OleDbCommandBuilder(adapter);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ds.Clear();
            adapter.Fill(ds, "good");  //语法上看不出来有什么错误
            d1.DataSource = ds.Tables["good"];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            adapter.Update(ds, "good");
        }

 
    }
}

PS:你可以百度一下'标准表达式中数据类型不匹配',里面有好多的案例,或许可以解决你的问题,很有可能是字段的数据类型搞错了。

[此贴子已经被作者于2016-2-25 10:50编辑过]

1