我知道这个问题很低级,呵,但小弟是刚学
提交到数据库表为user,字段为name,age
数据库组件有oledbconnection1和oledbcommand1,oledbconnection1已连接好ACCESS数据库
错误提示:错误1“System.Data.OleDb.OleDbCommand”并不包含“commandText”的定义F:\c#练习\lesson1\lesson1\Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.OleDb;
using System.Data.Common;
using System.Windows.Forms;
namespace lesson1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == " " || textBox2.Text.Trim() == " ")
{
MessageBox.Show( "姓名和年龄都不能为空! ");
return;
}
OleDbCommand cmd = new OleDbCommand1( "select * from user where name= ' " + textBox1.Text.Trim() + " ' ", oleDbConnection1);
if (null != cmd.ExecuteScalar())
MessageBox.Show( "这人已经输入过啦 ");
else
{
string sql = "insert into user (name,age) values ( ' " + textBox1.Text.Trim() + " ', ' " + textBox2.Text.Trim() + " ') ";
cmd.commandText = sql;
cmd.ExecuteNonQuery();
MessageBox.Show( "添加成功 ");
textBox1.Text = " ";
textBox2.Text = " ";
}
oleDbConnection1.Close();
}
}
}
[解决办法]
cmd.commandText = sql;
这里错了
[解决办法]
cmd.commandText = sql;
应该是大写的
[解决办法]
在这句话前边写 cmd.Connetion.open()
[解决办法]
之前要先打开连接oleDbConnection1.open();
好像这就话也有错误啊:OleDbCommand cmd = new OleDbCommand1( "select * from user where name= ' " + textBox1.Text.Trim() + " ' ", oleDbConnection1);