1. 在DataGridView中修改后的数据如何保存到数据库
namespace DategridviewTosql
{
public partial class Form1 : Form
{
private DataTable DT = new DataTable();
private SqlDataAdapter SDA = new SqlDataAdapter();
private Boolean isUpdate = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
load();
}
private void load()
{
SqlConnection conn = new SqlConnection(@"server = (local)\SQL2005;Integrated Security = true;" + "DataBase = test1");
SqlCommand SCD = new SqlCommand("select * from aaa ", conn);
SDA.SelectCommand = SCD;
SDA.Fill(DT);
dataGridView1.DataSource = DT;
}
private void button1_Click(object sender, EventArgs e)
{
if (isUpdate)
{
try
{
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
SDA.Update(DT);
isUpdate = false;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功! ");
}
else
{
MessageBox.Show("没有更新内容! ");
}
for (int i = 0; i < DT.Rows.Count; i++)
for (int j = 0; j < DT.Columns.Count; j++ )
{
dataGridView1[j, i].Style.BackColor = Color.White;
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
isUpdate = true;
dataGridView1[e.ColumnIndex,e.RowIndex].Style.BackColor = Color.Blue;
}
自己写的一个小实例,连接一个数据库,取了一个表显示,里面的链接语句可以根据你自己的数据库修改。试试看
2. C# 如何把datagridview里的数据写入数据库里
添加个按钮(保存)-编辑按钮的点击事件
SqlDataAdapter da = new SqlDataAdapter(sql,connection); //用于传输数据
SqlCommandBuilder buder = new SqlCommandBuilder (da); //用于生成SQL语句
da.Update(dataSet); 更新dataset中的数据
3. 怎么将dataGridView中的数据添加到SQL数据库中
在
sa.Update((DataTable)bs.DataSource);
之前添加
SqlCommandBuilderbu
=
new
SqlCommandBuilder(
sa);试一下。
我不知道你写那么多代码是要解决什么问题,如果是要将dataGridView中所作的变更都体现到DB中的话那么你可以尝试以下操作
1、用
SqlDataAdapter的
Fill方法填充一个
DataSet
2、将这个
DataSet
绑定到
dataGridView.DataSource
3、对
dataGridView
中的数据进行修改
4、用
SqlCommandBuilderbu
生成用于协调
DataSet
的更改与关联数据库的单表命令。
5、用
SqlDataAdapter的
Update
方法更新这个
DataSet
参考代码:
1、绑定
dataGridView
string
ConnectionString
=
"Data
Source=TEST;Persist
Security
Info=True;User
ID=my_new;Password=test";
DataSet
ds
=
new
DataSet();
OracleConnection
conn;
OracleDataAdapter
da;
da
=
new
OracleDataAdapter(
"select
t.*
from
cm_general_case_info_t
t",
conn
);
ds.Clear();
da.Fill(
ds,
"UserInfo"
);
conn.Close();
this.dataGridView1.DataSource
=
ds.Tables["UserInfo"];
2、更新dataGridView
DataTable
dt
=
(
DataTable
)dataGridView1.DataSource;
OracleCommandBuilder
bu
=
new
OracleCommandBuilder(
da
);
da.Update(
ds.Tables["UserInfo"]
);
我给的代码是用
ADO.NET
System.Data.OracleClient
命名空间
下的类,你只要改成对应的
System.Data.SqlClient
命名空间中的类即可。
4. 谁知道怎么把.dataGridView中读取的数据存入到数据库里
改成string a = this.dataGridView1.Rows[0].Cells[0].Value.ToString();追问:
就和你添加是一样的,SQL语句改成UPDATE就行了追问:
dataGridView 有个选中的功能,可以指定修改选中的行补充:
前提是你这行数据要存在唯一字段追问:
我们有个“修改”按钮,通过点击“修改”按钮,然后修改dataGridView中的数据的 不是直接在里面修改回答:
你前面已经说了是修改选中行的数据,既然是选中行的话,可以取到选中行的行数,然后取到这行数据在数据库存在的唯一字段,然后执行修改。至于你修改方式,因为看不到你界面,所以过程不好说,给你大概的步骤
1:获取选中行的行数3:执行修改
5. 如何把dataGridview里的数据全部保存到数据中 数据库是sqlserver
可以自定义数据类型,然后建立存储过程把这个类型设为参数,用程序调用这个参数把dataGridview里的数据加进去就行了,详细的操作可以网络搜一下
6. vb.net将DataGridView中的数据保存到SQL数据库
DimapAsSqlClient.SqlDataAdapter'这个变量很重要
PrivateFunctionSave_Data()AsBoolean'保存数据库的操作
Try
Me.DataGridView1.EndEdit()
'Me.BindingSource1=Me.DataGridView1.DataSource
Me.BindingSource1.EndEdit()
DimbdAsNewSqlClient.SqlCommandBuilder(ap)
ap.UpdateCommand=bd.GetUpdateCommand
ap.Update(Me.BindingSource1.DataSource)
Find(str_cmd)
ReturnTrue
CatchexAsException
MsgBox(ex.Message)
ReturnFalse
EndTry
EndFunction
7. c#的datagrid怎么将数据存到数据库
就是datagridview的行是否为新增还是修改的还是已保存(Unchange)
如下:
if
(this.dataGridView1.Rows[0].DataBoundItem==null)//未写到datatable的新行
{
}
else
{
if
(((DataRowView)this.dataGridView1.Rows[0].DataBoundItem).Row.RowState
==
DataRowState.Added)//
已写到datatable的新行
{
}
}
8. c# datagridview,怎么保存修改或新增数据到数据库
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.SqlClient;
namespace winform1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private SqlConnection cn;
private SqlCommandBuilder builder;
private SqlDataAdapter da;
private DataSet ds;
//查找事件,点击页面“查找”,
private void butSearch_Click(object sender, EventArgs e)
{
cn = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
cn.Open();
ds = new DataSet();
SqlCommand cmd = new SqlCommand("select * from Test", cn);
da = new SqlDataAdapter(cmd);
//添加必要的列和主键信息以守成架构
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
builder = new SqlCommandBuilder(da);
da.Fill(ds, "Test");//表名一定不能少哦。。。
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
//更新事件
private void butUpdate_Click(object sender, EventArgs e)
{
int row = da.Update(ds, "Test");
MessageBox.Show("更新完成" + row + builder.GetUpdateCommand().CommandText);
}
//插入新记录事件
private void btnInsert_Click(object sender, EventArgs e)
{
DataRow findRow = ds.Tables[0].Rows.Find("1111");//获取包含指定主键值的行
if (findRow != null)
{
MessageBox.Show("已有这个记录");
return;
}
DataRow dr = this.ds.Tables[0].NewRow();
dr["StuId"] = this.texStuId.Text;
dr["StuName"] = this.texStuName.Text;
dr["StuScore "] = "100";
this.ds.Tables[0].Rows.Add(dr);
int row = da.Update(ds, "Test");
MessageBox.Show("添加完成" + row + builder.GetInsertCommand().CommandText);
}
//删除选中记录
private void btnDelete_Click(object sender, EventArgs e)
{
ds.Tables[0].Rows[this.dataGridView1.CurrentRow.Index].Delete();
int row = da.Update(ds, "Test");
MessageBox.Show("删除完成" + row + builder.GetDeleteCommand().CommandText);
}
//查询事件
private void btnSearch_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from Test where StuId=@StuId", cn);
cmd.Parameters.Add("@StuId", SqlDbType.Char, 8);
cmd.Parameters["@StuId"].Value = this.texId.Text;
da = new SqlDataAdapter(cmd);
ds.Clear();
da.Fill(ds, "Test");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
}
9. 怎么将datagridview中修改后的数据通过按钮点击直接保存到数据库啊
1、首先在该按钮事件下编写保存方法
2、在保存方法当中获取当前需要保存的xml文档
3、按照相应的格式解析xml病取得其中的数据
4、讲数据存入相应的数据表
以上是大概的思路
其余的就是各个步骤的具体编码了
希望对你有帮助
10. datagridview 存储到数据库
public partial class Form1 : Form
{
private DataTable DT = new DataTable();
private SqlDataAdapter SDA = new SqlDataAdapter();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=127.0.0.1;database=pubs;uid=sa");
SqlCommand SCD = new SqlCommand("select * from tables", conn);
SDA.SelectCommand = SCD;
SDA.Fill(DT);
dataGridView1.DataSource = DT;
}
#region 使用Update更新数据库
private void button_save_Click(object sender, EventArgs e)
{
try
{
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
SDA.Update(DT);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功!");
}