dataGridView中的数据保存到数据库中

szyjp 2009-10-14 11:16:42
各位好,小弟想把dataGridView中的数据保存到数据库中,
现在dataGridView已可以正常显示数据了,但是在写到更新的地方遇到了一个问题:
写到Adapter.后面调不出update方法不知道为什么?希望指点一下.
下面将代面贴出来:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace newbook
{
public partial class Frm_adminuser : Form
{
public Frm_adminuser()
{
InitializeComponent();
}


bookDataSetTableAdapters.J_adminuserTableAdapter admin_ADP = new newbook.bookDataSetTableAdapters.J_adminuserTableAdapter();
bookDataSet.J_adminuserDataTable admin = new bookDataSet.J_adminuserDataTable();

private void Frm_adminuser_Load(object sender, EventArgs e)
{
//将数据填充到dataGridView1中:
admin_ADP.Fill(admin);
this.dataGridView1.DataSource = admin;
//数据可以正常显示!
}

//点击保存:将数据更新到数据库中
private void Save_Click(object sender, EventArgs e)
{
SqlCommandBuilder Builder = new SqlCommandBuilder(admin_ADP);
admin_ADP.
//这个地方调不出update方法!

}
}
}

...全文
145 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
休德芙 2010-05-07
  • 打赏
  • 举报
回复
小弟是新手,但是急需这个,大虾能不能把上面的代码多加一些注释啊,谢谢
szyjp 2009-10-26
  • 打赏
  • 举报
回复
呵呵,可能这个问题问的人太多了,所以都没有人回答了;
功夫不负有心人,我已在书上找到解决办法了.
现将代码公布在下面:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace temp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


SqlDataAdapter Adapter = null;
DataSet ds = new DataSet();
public static string sqlComm = "SELECT a.userid, a.[user_name], a.[password], a.admin, a.del, a.[add], a.[update], a.crdate, b.[user_name] as cruser FROM J_adminuser AS a , J_adminuser AS b where a.cruser = b.userid";
public static SqlConnection cn = new SqlConnection("server=IT01;database=book;uid=sa;pwd=123");
public static string userid = "";
public static int RowID =-1;

private void Form1_Load(object sender, EventArgs e)
{
getdate();
}

private void getdate()
{
//将数据填充到dataGridView1中:
Adapter = new SqlDataAdapter(sqlComm,cn);
Adapter.Fill(ds,"adminuser");
this.dataGridView1.DataSource=ds.Tables[0];

}

private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{

}
//点击保存按钮,执行插入语句和修改语句:
private void butSave_Click(object sender, EventArgs e)
{

try
{
SqlCommand Insert = new SqlCommand("insert into j_adminuser(userid,[user_name],[password],admin,del,[add],[update]) values (@userid,@user_name,@password,@admin,@del,@add,@update)", cn);
Insert.Parameters.Add("@userid", SqlDbType.NVarChar, 10, "userid");
Insert.Parameters.Add("@user_name", SqlDbType.NVarChar, 20, "user_name");
Insert.Parameters.Add("@password", SqlDbType.NVarChar, 20, "password");
Insert.Parameters.Add("@admin", SqlDbType.Bit, 1, "admin");
Insert.Parameters.Add("@del", SqlDbType.Bit, 1, "del");
Insert.Parameters.Add("@add", SqlDbType.Bit, 1, "add");
Insert.Parameters.Add("@update", SqlDbType.Bit, 1, "update");


SqlCommand Update = new SqlCommand("update j_adminuser set [user_name]=@user_name,[password]=@password,admin=@admin,del=@del,[add]=@add,[update]=@update where userid=@userid", cn);
Update.Parameters.Add("@user_name", SqlDbType.NVarChar, 20, "user_name");
Update.Parameters.Add("@password", SqlDbType.NVarChar, 20, "password");
Update.Parameters.Add("@admin", SqlDbType.Bit, 1, "admin");
Update.Parameters.Add("@del", SqlDbType.Bit, 1, "del");
Update.Parameters.Add("@add", SqlDbType.Bit, 1, "add");
Update.Parameters.Add("@update", SqlDbType.Bit, 1, "update");
Update.Parameters.Add("@userid", SqlDbType.NVarChar, 10, "userid");



Adapter.InsertCommand = Insert;
Adapter.UpdateCommand = Update;
SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(Adapter);
Adapter.Update(ds.Tables[0]);
//清空数据源
ds.Tables[0].Clear();
//刷新dataGridView1中的数据
getdate();
}
catch (DataException se)
{
MessageBox.Show(se.ToString());
}
}

//当用户添加行时,给复选框一个默认值:
private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
int i = this.dataGridView1.CurrentRow.Index;
this.dataGridView1.Rows[i].Cells[3].Value = false;
this.dataGridView1.Rows[i].Cells[4].Value = false;
this.dataGridView1.Rows[i].Cells[5].Value = false;
this.dataGridView1.Rows[i].Cells[6].Value = false;
}
//删除记录:
private void butDel_Click(object sender, EventArgs e)
{
if (userid!="")
{
DialogResult Result = MessageBox.Show("确定要删除:"+userid+"这个用户吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);


if (Result==DialogResult.OK)
{

ds.Tables[0].Rows[RowID].Delete();
SqlCommand Delete = new SqlCommand("delete from j_adminuser where userid='"+userid+"'", cn);
//Delete.Parameters.Add("@userid", SqlDbType.NVarChar, 10, "userid");

Adapter.DeleteCommand = Delete;
SqlCommandBuilder Builder = new SqlCommandBuilder(Adapter);
Adapter.Update(ds.Tables[0]);
ds.Tables[0].Clear();
MessageBox.Show("删除成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
userid = "";
getdate();
MessageBox.Show("已刷新!");
}
}
else
{
MessageBox.Show("请先选择要删除的记录!","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk);
}
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
userid = "";
RowID = this.dataGridView1.CurrentRow.Index;
userid = Convert.ToString(this.dataGridView1.Rows[RowID].Cells[0].Value);

}

private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{

}



}
}

古今多少事 2009-10-14
  • 打赏
  • 举报
回复
直接输进去看看有什么提示没有,然后判断问题……
szyjp 2009-10-14
  • 打赏
  • 举报
回复
怎么没有人回答我啊,还是分不够请直说,不够可以再加!

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧