C#窗体实现SQL的增删改查功能

我是c#新手,老师给了个题目,需要实现以上功能,
窗口布局textbox1对应employee表的id,textbox2为name
textbox3为sex,textbox4为addr,textbox5为brithday
设置“查询”按钮实现 在dataGridView1中显示表employee的所有数据,
“插入”“修改”“删除”等功能,有能力的大虾帮忙给下代码,包括连接数据库和using加载项,50分奉上,能让我过关再加50

帮你写个最简单的程序吧,已经在VS2005环境下编译通过:
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.OracleClient;

namespace DataGridView显示数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnQuery_Click(object sender, EventArgs e)
{
string sql = "SELECT factory_id,factory_name,bank_id,bank_name FROM B_FACTORY WHERE valid='1'";//这里改相应的SQL语句就可以实现相应的功能
DataSet ds = DBcommand(sql);
dataGridView1.DataSource = ds.Tables[0];
}

/// <summary>
/// 对oracle数据库进行操作
/// </summary>
/// <param name="command">SQL查询语句</param>
private DataSet DBcommand(string command)
{
string connectionString = "Data Source=MTMS;user id=admin;password=admin"; //改为你自己的数据库连接
OracleConnection conn = new OracleConnection();
conn.ConnectionString = connectionString;

try
{
conn.Open();
OracleCommand cmd = new OracleCommand(command, conn);
OracleDataAdapter da = new OracleDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "movie");
conn.Close();
return ds;
}
catch (Exception ex)
{
MessageBox.Show("打开数据库失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
finally
{
conn.Close();
}
}

private void btnDel_Click(object sender, EventArgs e)
{

}
}
}
说明几点:
1.界面自己画
2.我这里只有oracle数据库,如果是其它数据库,连接方法应该类似,自己到网上找找
3.为了调试我随便找了个表B_factory,表结构factory_id,factory_name,bank_id,bank_name ,你相应改为employee表就是了
4.快下班了只做了个查询功能,但已经实现了最重要的数据库连接和datagridview数据绑定,要实现增加和删除只要直接改那个sql语句就行了;修改稍微麻烦点,把选中的行放入一个临时变量,修改后同样写个update的sql语句传进去就行了
最后祝你过关
------------------
补充一点,如果编译通不过可能要手动添加引用:右键单击项目,选添加引用,或右键单击引用,然后在弹出来的添加引用对话框里的.NET选项卡中选择System.Data.OracleClient,点确定就行了
温馨提示:内容为网友见解,仅供参考
第1个回答  2008-09-05
给你写个例子吧!
希望你能研究懂!

首先要对sqlclient引用;
程序头文件加上using System.Data.Sqlclient

程序中:
set conn=server.createobject("adodb.connection")
connstr="driver={SQL Server};server=local; uid=jsyc;pwd=123456;database=jzzs"
打开数据库:conn.open connstr
关闭呢是close

执行sql来完成增 删 查 改
string StrSQL = "INSERT INTO feed(id,mealie_buy,mealie_use,bean_buy,bean_use,medicine_buy,medicine_use,remark) VALUES('" + aa + "','" + bb + "','" + cc + "','" + dd + "','" + ee + "','" + ff + "','" + gg + "','" + h + "')";
SqlCommand cmd = new SqlCommand(StrSQL, myconn);

try
{

myconn.Open(); //打开数据库连接

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
da.Update(ds);
myconn.Close(); //关闭数据库连接
MessageBox.Show("增加成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception Err)
{
MessageBox.Show("增加操作失败:" + Err.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
第2个回答  2008-09-05
DataGridView用法示例2008-06-28 13:43private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView view = (DataGridView)sender; //可将参数sender显示转化为DataGridView实例
label1.Text = "留言内容";
DataGridViewRow row = view.Rows[e.RowIndex]; //获取行,获取行中的具体的值,rows属性可获得DataGridViewRow中row 的一个集合,e.rowindex为点击的行号
//MessageBox.Show(row.Cells[0].Value.ToString() + row.Cells[1].Value.ToString() + row.Cells[2].Value.ToString() + row.Cells[3].Value.ToString());
this.dgv.Hide();
this.label1.Visible = true;
this.label2.Visible = true;
this.label3.Visible = true;
this.label4.Visible = true;
this.label5.Visible = true;
this.label6.Visible = true;
this.label7.Visible = true;
this.button1.Visible = true;
this.button2.Visible = false;
this.re.Visible = true;
this.textBox2.Visible = true;
this.textBox2.ReadOnly = true;
this.label2.Text = row.Cells[0].Value.ToString().Trim();
this.label3.Text = row.Cells[1].Value.ToString().Trim();
this.label4.Text = row.Cells[3].Value.ToString().Trim();

DataTable dtl = this.v3ta.GetDataBy(this.label2.Text.Trim(), this.label4.Text);//v3ta是一个TableAdapter的对象,GetDataBy是它的一个查询,返回一个datatable对象
if (dtl.Rows.Count != 0)
{
this.textBox2.Text = dtl.Rows[0].ItemArray[5].ToString().Trim() + "\r\n" + "----------------------------------------------------";//table对象dtl,也可以用rows,来获取一个ROW的集合,然后用索引访问每一个row

然后用row的ItemArray[index],访问row的每一个单元
int i;
for (i = 0; i < dtl.Rows.Count; i++)
this.textBox2.Text += "\r\n" + dtl.Rows[i].ItemArray[3].ToString().Trim() + "\t" + dtl.Rows[i].ItemArray[4].ToString().Trim() + "\t回复:\r\n\r\n" + dtl.Rows[i].ItemArray[2].ToString().Trim() + "\r\n";
}

else
this.textBox2.Text = row.Cells[2].Value.ToString();
第3个回答  2008-09-06
多看下就知道了,很简单的
第4个回答  2008-09-05
就是几个SQL语句啊兄弟,网上这种教程多的是。唉
相似回答