选择 text 进行输入数据在button按钮的 button1_Click 事件中添加连接数据库和inset语句就可以添加到相应的表中我也是个新手 见笑了~~~~
SqlDataAdapter可以将本地DataSet中的数据修改返回到数据库中。试试看吧。
我的方法是:做个实体类Stu 获得你选中行的数据然后封装进去 最后往DB里插入 foreach(DataGirdViewRow dr in dataGirdView1.SelectRows) { u_id=dr.cell[0].Value.ToString(); u_user=dr.cell[1].Value.ToString(); u_code=dr.cell[2].Value.ToString(); Stu stu=new Stu(); stu.id=u_id; stu.user=u_user; set.code=u_code; list.Add(stu) } 调用DAL里的Add方法 返回影响的行数 if(i>0) MessageBox.show("成功") else MessageBox.show("失败") 在DAL层里调用DBHelper类的Execute方法执行插入 public static int Add(Stu stu) { sql=insert into 表名 values(@id,@user,@code) Sqlparameter[] pare=new Sqlparameter[] { new SqlParameter("@id",stu.id), new SqlParameter("@user",stu.user), new SqlParameter("@code",stu.code) } int i=DBHelper.Execute(sql,pare) }
简单数据库操作示例,楼主参考下,数据库用的ACCESS,SQL其实也差不多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 WindowsApplication4 { public partial class Form1 : Form { OleDbDataAdapter da; DataTable dt; OleDbConnection cn; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { da.Update(dt); } private void Form1_Load(object sender, EventArgs e) { cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Northwind.mdb"); da = new OleDbDataAdapter("select * from orders", cn); OleDbCommandBuilder cb = new OleDbCommandBuilder(da); dt = new DataTable(); da.Fill(dt); dataGridView1.DataSource = dt; } } }
string dfs = @"server=127.0.0.1;user id=sa;password=password;database=database"; SqlConnection dbconn = new SqlConnection(dfs);
try { dbconn.Open(); } catch { MessageBox.Show("数据库连接错误\n请检查网络状态!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } //检查有无数据 DataSet ds=new DataSet (); string oda="select * from amply where O_ctn='"+txtCTNNo.Text.Trim()+"'"; SqlDataAdapter df = new SqlDataAdapter(oda,dbconn); df.Fill(ds,"amply"); if (ds.Tables[0].Rows.Count > 0) { MessageBox.Show("数据重复!") return; } //插入Sql数据 string cmd="INSERT INTO amply (O_SMN,O_CTN,O_MO,O_MFT,O_User,O_DataTime) VALUES ('"+txtNo.Text.Trim()+"','"+txtCTNNo.Text.Trim()+"','"+txtCTNNo.Text.Substring(1,5)+"','"+txtStie.Text.Trim()+"','"+this.lblName.Text.Trim()+"','"+lblDATATime.Text.Trim()+"')"; SqlCommand dd = new SqlCommand(cmd,dbconn); dd.ExecuteNonQuery(); dbconn.Close(); MessageBox.Show("成功!") return;
选择 text 进行输入数据在button按钮的 button1_Click 事件中添加连接数据库和inset语句就可以添加到相应的表中我也是个新手 见笑了~~~~
我的方法是:做个实体类Stu 获得你选中行的数据然后封装进去 最后往DB里插入
foreach(DataGirdViewRow dr in dataGirdView1.SelectRows)
{
u_id=dr.cell[0].Value.ToString();
u_user=dr.cell[1].Value.ToString();
u_code=dr.cell[2].Value.ToString();
Stu stu=new Stu();
stu.id=u_id;
stu.user=u_user;
set.code=u_code;
list.Add(stu)
}
调用DAL里的Add方法 返回影响的行数
if(i>0) MessageBox.show("成功")
else MessageBox.show("失败")
在DAL层里调用DBHelper类的Execute方法执行插入
public static int Add(Stu stu)
{
sql=insert into 表名 values(@id,@user,@code)
Sqlparameter[] pare=new Sqlparameter[]
{
new SqlParameter("@id",stu.id),
new SqlParameter("@user",stu.user),
new SqlParameter("@code",stu.code)
}
int i=DBHelper.Execute(sql,pare)
}
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 WindowsApplication4
{
public partial class Form1 : Form
{
OleDbDataAdapter da;
DataTable dt;
OleDbConnection cn; public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
da.Update(dt);
} private void Form1_Load(object sender, EventArgs e)
{
cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Northwind.mdb"); da = new OleDbDataAdapter("select * from orders", cn); OleDbCommandBuilder cb = new OleDbCommandBuilder(da); dt = new DataTable(); da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
string dfs = @"server=127.0.0.1;user id=sa;password=password;database=database";
SqlConnection dbconn = new SqlConnection(dfs);
try
{
dbconn.Open();
}
catch
{
MessageBox.Show("数据库连接错误\n请检查网络状态!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
return;
}
//检查有无数据
DataSet ds=new DataSet ();
string oda="select * from amply where O_ctn='"+txtCTNNo.Text.Trim()+"'";
SqlDataAdapter df = new SqlDataAdapter(oda,dbconn);
df.Fill(ds,"amply");
if (ds.Tables[0].Rows.Count > 0)
{
MessageBox.Show("数据重复!")
return;
}
//插入Sql数据
string cmd="INSERT INTO amply (O_SMN,O_CTN,O_MO,O_MFT,O_User,O_DataTime) VALUES ('"+txtNo.Text.Trim()+"','"+txtCTNNo.Text.Trim()+"','"+txtCTNNo.Text.Substring(1,5)+"','"+txtStie.Text.Trim()+"','"+this.lblName.Text.Trim()+"','"+lblDATATime.Text.Trim()+"')";
SqlCommand dd = new SqlCommand(cmd,dbconn);
dd.ExecuteNonQuery();
dbconn.Close();
MessageBox.Show("成功!")
return;