求一个c#做的winform 小程序
求一个c#做的winform小程序。要有数据库的读取和添加,修改,删除等代码。我想参考一下。谢谢!...
求一个c#做的winform 小程序。要有数据库的读取和添加,修改,删除等代码。
我想参考一下。
谢谢! 展开
我想参考一下。
谢谢! 展开
5个回答
展开全部
这是当年老师给的例题
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ex12_2
{
public partial class Form1 : Form
{
OleDbConnection myCon ;
OleDbDataAdapter myDA;
DataSet myDS ;
DataTable tab1 = new DataTable();
int n; //当前记录的索引值
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
myCon=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=学生.mdb");
myCon.Open();
string s = "select * from 成绩";
myDA = new OleDbDataAdapter(s,myCon );
//自动生成SQL命令,以便更新数据源
OleDbCommandBuilder builder = new OleDbCommandBuilder(myDA);
myDS = new DataSet();
myDA.Fill(myDS, "成绩表");
tab1 = myDS.Tables["成绩表"];
//MessageBox.Show(tab1.Rows.Count.ToString());
if (tab1.Rows.Count > 0)
{
n = 0;
textBox1.Text = tab1.Rows[0]["学号"].ToString();
textBox2.Text = tab1.Rows[0]["姓名"].ToString();
textBox3.Text = tab1.Rows[0]["成绩"].ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
//单击"后一条"按钮
if (n < tab1.Rows.Count - 1)
{
n++;
textBox1.Text = tab1.Rows[n]["学号"].ToString();
textBox2.Text = tab1.Rows[n]["姓名"].ToString();
textBox3.Text = tab1.Rows[n]["成绩"].ToString();
}
else
MessageBox.Show("当前记录是最后一条记录");
}
private void button1_Click(object sender, EventArgs e)
{
//单击"前一条"按钮
if (n > 0)
{
n--;
textBox1.Text = tab1.Rows[n]["学号"].ToString();
textBox2.Text = tab1.Rows[n]["姓名"].ToString();
textBox3.Text = tab1.Rows[n]["成绩"].ToString();
}
else
MessageBox.Show("当前记录是第一条记录");
}
private void button3_Click(object sender, EventArgs e)
{
//单击"插入记录"
DataRow dr=tab1.NewRow(); //使用tab1的结构生成一个新行
dr["学号"] = textBox5.Text;
dr["姓名"] = textBox6.Text;
dr["成绩"] = textBox4.Text;
tab1.Rows.Add(dr); //将新行追加到tab1表的末尾
myDA.Update(myDS, "成绩表");
n = tab1.Rows.Count - 1;
textBox1.Text = textBox5.Text;
textBox2.Text = textBox6.Text;
textBox3.Text = textBox4.Text;
}
private void button6_Click(object sender, EventArgs e)
{
//退出
Close();
}
private void button4_Click(object sender, EventArgs e)
{
//删除记录
if(MessageBox.Show("确实要删除当前记录么?","提示",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
tab1.Rows[n].Delete();
myDA.Update(myDS, "成绩表");
//删除记录后,显示被删记录的下一条记录
if (n < tab1.Rows.Count - 1)
n++;
else if (n > 0)
n--;
else
{
MessageBox.Show("表中已无记录");
return;
}
textBox1.Text = tab1.Rows[n]["学号"].ToString();
textBox2.Text = tab1.Rows[n]["姓名"].ToString();
textBox3.Text = tab1.Rows[n]["成绩"].ToString();
}
}
private void button5_Click(object sender, EventArgs e)
{
//更新记录
tab1.Rows[n]["学号"] = textBox1.Text;
tab1.Rows[n]["姓名"] = textBox2.Text;
tab1.Rows[n]["成绩"] = textBox3.Text;
myDA.Update(myDS, "成绩表");
}
private void button7_Click(object sender, EventArgs e)
{
//按姓名查找
myDS.Clear();
OleDbCommand com1=new OleDbCommand();
com1.CommandText= "select * from 成绩 where 姓名='" + textBox7.Text + "'";
com1.Connection = myCon;
myDA.SelectCommand = com1;
myDA.Fill(myDS, "成绩表");
tab1 = myDS.Tables["成绩表"];
if (tab1.Rows.Count > 0)
{
n = 0;
textBox1.Text = tab1.Rows[0]["学号"].ToString();
textBox2.Text = tab1.Rows[0]["姓名"].ToString();
textBox3.Text = tab1.Rows[0]["成绩"].ToString();
}
else
MessageBox.Show("查无此人");
}
private void button8_Click(object sender, EventArgs e)
{
//重新显示
myDS.Clear();
OleDbCommand com1 = new OleDbCommand();
com1.CommandText = "select * from 成绩";
com1.Connection = myCon ;
myDA.SelectCommand = com1;
myDA.Fill(myDS, "成绩表");
tab1 = myDS.Tables["成绩表"];
if (tab1.Rows.Count > 0)
{
n = 0;
textBox1.Text = tab1.Rows[0]["学号"].ToString();
textBox2.Text = tab1.Rows[0]["姓名"].ToString();
textBox3.Text = tab1.Rows[0]["成绩"].ToString();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ex12_2
{
public partial class Form1 : Form
{
OleDbConnection myCon ;
OleDbDataAdapter myDA;
DataSet myDS ;
DataTable tab1 = new DataTable();
int n; //当前记录的索引值
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
myCon=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=学生.mdb");
myCon.Open();
string s = "select * from 成绩";
myDA = new OleDbDataAdapter(s,myCon );
//自动生成SQL命令,以便更新数据源
OleDbCommandBuilder builder = new OleDbCommandBuilder(myDA);
myDS = new DataSet();
myDA.Fill(myDS, "成绩表");
tab1 = myDS.Tables["成绩表"];
//MessageBox.Show(tab1.Rows.Count.ToString());
if (tab1.Rows.Count > 0)
{
n = 0;
textBox1.Text = tab1.Rows[0]["学号"].ToString();
textBox2.Text = tab1.Rows[0]["姓名"].ToString();
textBox3.Text = tab1.Rows[0]["成绩"].ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
//单击"后一条"按钮
if (n < tab1.Rows.Count - 1)
{
n++;
textBox1.Text = tab1.Rows[n]["学号"].ToString();
textBox2.Text = tab1.Rows[n]["姓名"].ToString();
textBox3.Text = tab1.Rows[n]["成绩"].ToString();
}
else
MessageBox.Show("当前记录是最后一条记录");
}
private void button1_Click(object sender, EventArgs e)
{
//单击"前一条"按钮
if (n > 0)
{
n--;
textBox1.Text = tab1.Rows[n]["学号"].ToString();
textBox2.Text = tab1.Rows[n]["姓名"].ToString();
textBox3.Text = tab1.Rows[n]["成绩"].ToString();
}
else
MessageBox.Show("当前记录是第一条记录");
}
private void button3_Click(object sender, EventArgs e)
{
//单击"插入记录"
DataRow dr=tab1.NewRow(); //使用tab1的结构生成一个新行
dr["学号"] = textBox5.Text;
dr["姓名"] = textBox6.Text;
dr["成绩"] = textBox4.Text;
tab1.Rows.Add(dr); //将新行追加到tab1表的末尾
myDA.Update(myDS, "成绩表");
n = tab1.Rows.Count - 1;
textBox1.Text = textBox5.Text;
textBox2.Text = textBox6.Text;
textBox3.Text = textBox4.Text;
}
private void button6_Click(object sender, EventArgs e)
{
//退出
Close();
}
private void button4_Click(object sender, EventArgs e)
{
//删除记录
if(MessageBox.Show("确实要删除当前记录么?","提示",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
tab1.Rows[n].Delete();
myDA.Update(myDS, "成绩表");
//删除记录后,显示被删记录的下一条记录
if (n < tab1.Rows.Count - 1)
n++;
else if (n > 0)
n--;
else
{
MessageBox.Show("表中已无记录");
return;
}
textBox1.Text = tab1.Rows[n]["学号"].ToString();
textBox2.Text = tab1.Rows[n]["姓名"].ToString();
textBox3.Text = tab1.Rows[n]["成绩"].ToString();
}
}
private void button5_Click(object sender, EventArgs e)
{
//更新记录
tab1.Rows[n]["学号"] = textBox1.Text;
tab1.Rows[n]["姓名"] = textBox2.Text;
tab1.Rows[n]["成绩"] = textBox3.Text;
myDA.Update(myDS, "成绩表");
}
private void button7_Click(object sender, EventArgs e)
{
//按姓名查找
myDS.Clear();
OleDbCommand com1=new OleDbCommand();
com1.CommandText= "select * from 成绩 where 姓名='" + textBox7.Text + "'";
com1.Connection = myCon;
myDA.SelectCommand = com1;
myDA.Fill(myDS, "成绩表");
tab1 = myDS.Tables["成绩表"];
if (tab1.Rows.Count > 0)
{
n = 0;
textBox1.Text = tab1.Rows[0]["学号"].ToString();
textBox2.Text = tab1.Rows[0]["姓名"].ToString();
textBox3.Text = tab1.Rows[0]["成绩"].ToString();
}
else
MessageBox.Show("查无此人");
}
private void button8_Click(object sender, EventArgs e)
{
//重新显示
myDS.Clear();
OleDbCommand com1 = new OleDbCommand();
com1.CommandText = "select * from 成绩";
com1.Connection = myCon ;
myDA.SelectCommand = com1;
myDA.Fill(myDS, "成绩表");
tab1 = myDS.Tables["成绩表"];
if (tab1.Rows.Count > 0)
{
n = 0;
textBox1.Text = tab1.Rows[0]["学号"].ToString();
textBox2.Text = tab1.Rows[0]["姓名"].ToString();
textBox3.Text = tab1.Rows[0]["成绩"].ToString();
}
}
}
}
展开全部
里面有例子!你新建一个例子程序就可以了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
MD
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你发zwei61@tom.com我晚上发给你
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询