using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;namespace Csharp第五章
{
public partial class 新增学生信息 : Form
{
public 新增学生信息()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result;
result = MessageBox.Show("确定退出?", "退出提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
this.Close();
}
}
private static string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\projects\School\MySchool.accdb;Persist Security Info=False";
public static OleDbConnection connection = new OleDbConnection(connString); private void button1_Click(object sender, EventArgs e)
{
string userName = textBox1.Text;
string password = textBox2.Text;
string name = textBox4.Text;
int age = Convert.ToInt32(textBox5.Text);
string adress = textBox6.Text;
int tel = Convert.ToInt32(textBox7.Text);
string email = textBox8.Text;
string specialty = comboBox1.Text;
string Cclass = comboBox2.Text;
string sql = "insert into 新增学生信息(userName,passWord,state,name,age,adress,tel,email,sex,specialty,cclass) values('" + userName + "','" + password+ "',1,'" + name + "','" + age+ "','" + adress + "','" + tel + "','" + email + "','" + '男' + "','" + specialty+ "','" +Cclass+ "')";
using (OleDbCommand command = new OleDbCommand(sql))
{
connection.Open();
MessageBox.Show("打开数据库成功");
int reselt = command.ExecuteNonQuery(); //这里会有"ExecuteNonQuery: Connection 属性尚未初始化。"报错!
if (reselt !=1)
{
MessageBox.Show("添加失败!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show("添加成功!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
connection.Close();
}
} }
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;namespace Csharp第五章
{
public partial class 新增学生信息 : Form
{
public 新增学生信息()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result;
result = MessageBox.Show("确定退出?", "退出提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
this.Close();
}
}
private static string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\projects\School\MySchool.accdb;Persist Security Info=False";
public static OleDbConnection connection = new OleDbConnection(connString); private void button1_Click(object sender, EventArgs e)
{
string userName = textBox1.Text;
string password = textBox2.Text;
string name = textBox4.Text;
int age = Convert.ToInt32(textBox5.Text);
string adress = textBox6.Text;
int tel = Convert.ToInt32(textBox7.Text);
string email = textBox8.Text;
string specialty = comboBox1.Text;
string Cclass = comboBox2.Text;
string sql = "insert into 新增学生信息(userName,passWord,state,name,age,adress,tel,email,sex,specialty,cclass) values('" + userName + "','" + password+ "',1,'" + name + "','" + age+ "','" + adress + "','" + tel + "','" + email + "','" + '男' + "','" + specialty+ "','" +Cclass+ "')";
using (OleDbCommand command = new OleDbCommand(sql))
{
connection.Open();
MessageBox.Show("打开数据库成功");
int reselt = command.ExecuteNonQuery(); //这里会有"ExecuteNonQuery: Connection 属性尚未初始化。"报错!
if (reselt !=1)
{
MessageBox.Show("添加失败!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show("添加成功!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
connection.Close();
}
} }
}
和sql的一样。