C#每天抄一点(37):动态增加数据库信息
01 Nov 2011[music1g play=#409418 autoplay=false]
插入了解了,删,改,查也差不多了吧。
MainForm.cs
01 /*
02 * 由SharpDevelop创建。
03 * 用户: Lazynight
04 * 日期: 2011/11/1
05 * 时间: 12:59
06 *
07 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
08 */
09 using System;
10 using System.Collections.Generic;
11 using System.Drawing;
12 using System.Windows.Forms;
13 using System.Data;
14 using System.Data.OleDb;
15 namespace Lazy37动态增加数据库信息
16 {
17
18 public partial class MainForm : Form
19 {
20 string Lazy_user;//账号
21 string Lazy_pwd;//密码
22 string Lazy_pow;//权限
23 public MainForm()
24 {
25
26 InitializeComponent();
27
28 }
29
30 void Button1Click(object sender, EventArgs e)
31 {
32 if(textBox1.Text==“”||textBox2.Text==“”||textBox3.Text==“”)
33 {
34 MessageBox.Show(“输入的信息不完整,请重新输入~”,“提示”,MessageBoxButtons.OK,MessageBoxIcon.Information);
35 }
36 else
37 {
38 string Lazy_path=Application.StartupPath+“\\Lazy_Data.accdb”;
39 string Lazy_constr=“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=”+Lazy_path;
40 OleDbConnection Lazy_connect=new OleDbConnection(Lazy_constr);
41 Lazy_connect.Open();
42 string Lazy_insert=“insert into Person(账号,密码,权限) values ( ‘” + textBox1.Text + “‘,’” + textBox2.Text + “‘,” + Convert.ToInt32(textBox3.Text) + “)”;
43 OleDbCommand Lazy_command=new OleDbCommand(Lazy_insert,Lazy_connect);
44 Lazy_command.ExecuteNonQuery();
45 Lazy_connect.Close();
46 Lazy_connect.Dispose();
47 MessageBox.Show(“已成功想数据库插入一条记录~”,“提示”,MessageBoxButtons.OK,MessageBoxIcon.Information);
48 Lazy_user=textBox1.Text;
49 Lazy_pwd=textBox2.Text;
50 Lazy_pow=textBox3.Text;
51 textBox1.Text=“”;
52 textBox2.Text=“”;
53 textBox3.Text=“”;
54 }
55 }
56
57 void Button2Click(object sender, EventArgs e)
58 {
59 Form1 Lazy_form=new Form1();
60 Lazy_form.toolStripStatusLabel1.Text=“成功插入一条记录->账号:”+Lazy_user+“,密码:”+Lazy_pwd+“,权限:”+Lazy_pow;
61 Lazy_form.ShowDialog();
62 }
63
64 void Button3Click(object sender, EventArgs e)
65 {
66 this.Close();
67 Application.Exit();
68 }
69 }
70 }
02 * 由SharpDevelop创建。
03 * 用户: Lazynight
04 * 日期: 2011/11/1
05 * 时间: 12:59
06 *
07 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
08 */
09 using System;
10 using System.Collections.Generic;
11 using System.Drawing;
12 using System.Windows.Forms;
13 using System.Data;
14 using System.Data.OleDb;
15 namespace Lazy37动态增加数据库信息
16 {
17
18 public partial class MainForm : Form
19 {
20 string Lazy_user;//账号
21 string Lazy_pwd;//密码
22 string Lazy_pow;//权限
23 public MainForm()
24 {
25
26 InitializeComponent();
27
28 }
29
30 void Button1Click(object sender, EventArgs e)
31 {
32 if(textBox1.Text==“”||textBox2.Text==“”||textBox3.Text==“”)
33 {
34 MessageBox.Show(“输入的信息不完整,请重新输入~”,“提示”,MessageBoxButtons.OK,MessageBoxIcon.Information);
35 }
36 else
37 {
38 string Lazy_path=Application.StartupPath+“\\Lazy_Data.accdb”;
39 string Lazy_constr=“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=”+Lazy_path;
40 OleDbConnection Lazy_connect=new OleDbConnection(Lazy_constr);
41 Lazy_connect.Open();
42 string Lazy_insert=“insert into Person(账号,密码,权限) values ( ‘” + textBox1.Text + “‘,’” + textBox2.Text + “‘,” + Convert.ToInt32(textBox3.Text) + “)”;
43 OleDbCommand Lazy_command=new OleDbCommand(Lazy_insert,Lazy_connect);
44 Lazy_command.ExecuteNonQuery();
45 Lazy_connect.Close();
46 Lazy_connect.Dispose();
47 MessageBox.Show(“已成功想数据库插入一条记录~”,“提示”,MessageBoxButtons.OK,MessageBoxIcon.Information);
48 Lazy_user=textBox1.Text;
49 Lazy_pwd=textBox2.Text;
50 Lazy_pow=textBox3.Text;
51 textBox1.Text=“”;
52 textBox2.Text=“”;
53 textBox3.Text=“”;
54 }
55 }
56
57 void Button2Click(object sender, EventArgs e)
58 {
59 Form1 Lazy_form=new Form1();
60 Lazy_form.toolStripStatusLabel1.Text=“成功插入一条记录->账号:”+Lazy_user+“,密码:”+Lazy_pwd+“,权限:”+Lazy_pow;
61 Lazy_form.ShowDialog();
62 }
63
64 void Button3Click(object sender, EventArgs e)
65 {
66 this.Close();
67 Application.Exit();
68 }
69 }
70 }
Form1.cs
01 /*
02 * 由SharpDevelop创建。
03 * 用户: Lazynight
04 * 日期: 2011/11/1
05 * 时间: 14:53
06 *
07 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
08 */
09 using System;
10 using System.Drawing;
11 using System.Windows.Forms;
12 using System.Data;
13 using System.Data.OleDb;
14 namespace Lazy37动态增加数据库信息
15 {
16
17 public partial class Form1 : Form
18 {
19 public Form1()
20 {
21
22 InitializeComponent();
23 }
24 void Form1Load(object sender, EventArgs e)
25 {
26 string Lazy_path=Application.StartupPath+“\\Lazy_Data.accdb”;
27 string Lazy_constr=“Provider = Microsoft.ACE.OLEDB.12.0; Data Source =”+Lazy_path;
28 OleDbConnection Lazy_connect=new OleDbConnection(Lazy_constr);
29 string Lazy_sql=“select * from Person”;
30 OleDbDataAdapter Lazy_data=new OleDbDataAdapter(Lazy_sql,Lazy_connect);
31 DataSet Lazy_set=new DataSet();
32 Lazy_data.Fill(Lazy_set);
33 dataGridView1.DataSource=Lazy_set.Tables[].DefaultView;
34 Lazy_connect.Close();
35 Lazy_connect.Dispose();
36 }
37
38
39 }
40 }
02 * 由SharpDevelop创建。
03 * 用户: Lazynight
04 * 日期: 2011/11/1
05 * 时间: 14:53
06 *
07 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
08 */
09 using System;
10 using System.Drawing;
11 using System.Windows.Forms;
12 using System.Data;
13 using System.Data.OleDb;
14 namespace Lazy37动态增加数据库信息
15 {
16
17 public partial class Form1 : Form
18 {
19 public Form1()
20 {
21
22 InitializeComponent();
23 }
24 void Form1Load(object sender, EventArgs e)
25 {
26 string Lazy_path=Application.StartupPath+“\\Lazy_Data.accdb”;
27 string Lazy_constr=“Provider = Microsoft.ACE.OLEDB.12.0; Data Source =”+Lazy_path;
28 OleDbConnection Lazy_connect=new OleDbConnection(Lazy_constr);
29 string Lazy_sql=“select * from Person”;
30 OleDbDataAdapter Lazy_data=new OleDbDataAdapter(Lazy_sql,Lazy_connect);
31 DataSet Lazy_set=new DataSet();
32 Lazy_data.Fill(Lazy_set);
33 dataGridView1.DataSource=Lazy_set.Tables[].DefaultView;
34 Lazy_connect.Close();
35 Lazy_connect.Dispose();
36 }
37
38
39 }
40 }
转载请注明:于哲的博客 » C#每天抄一点(37):动态增加数据库信息