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("更新成功!");
}