A. C# 一次性保存DataGridView中的數據
namespace DataTest
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myTableTableAdapter.Update(myDBDataSet);//這里就是保存全部的內容。
}
private void FormMain_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數據載入到表「myDBDataSet.MyTable」中。您可以根據需要移動或移除它。
this.myTableTableAdapter.Fill(this.myDBDataSet.MyTable);
}
}
}
我的做法是:使用DataSet控制項,和DataGridView,全都是控制項,代碼就是這點
private void button1_Click(object sender, EventArgs e)
{
myTableTableAdapter.Update(myDBDataSet);//這里就是保存全部的內容。
}
B. 怎麼將datagridview中修改後的數據通過按鈕點擊直接保存到資料庫啊
1、首先在該按鈕事件下編寫保存方法
2、在保存方法當中獲取當前需要保存的xml文檔
3、按照相應的格式解析xml病取得其中的數據
4、講數據存入相應的數據表
以上是大概的思路
其餘的就是各個步驟的具體編碼了
希望對你有幫助
C. datagridview 保存數據
有問題在告訴我
SqlConnection con = new SqlConnection("data source=.;initial catalog=XJGL;Integrated Security=SSPI");
string sql = "insert into right(欄位,欄位,欄位,欄位。。。) values('值','值',。。。。。。)";
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
cmd.ExecuteNonQuery();
con.Close();
D. C# 保存DataGridView中的數據
代碼還是自己多寫 ,給你說思路和需要注意的地方。
1.datagridview 是要可以編輯,添加,刪除的模式
2.在界面上放置一個控制項 點擊後保存,當然也可以在DataGridView的事件裡面寫,但是這樣比較麻煩
3.隨意改,在點擊保存按鈕後,注意這里需要調用datagridview.EndEdit() 要不你編輯的東西是無效的,然後再去遍歷datagridview中的數據和資料庫中的比對 沒有的就添加,有的就修改,沒了的就刪除
E. winform中dataGridView上怎麼修改、保存數據啊,急用啊
1、如果datagridview1的屬性selectionmode是fullrowselect的話,就
datagridview1.currentrow.cells["n_c"].value
=
textbox1.text.tostring().trim();
2、如果datagridview1的屬性selectionmode是cellselect的話,就
datagridview1.currentcell["n_c"].value
=
textbox1.text.tostring().trim();
然後就是保存到資料庫了,具體也分sql或者access。
F. 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;
}
}
}
G. 用戶在查看datagridview中的數據時,可以對datagridview中的數據進行增刪改操作,操作後如何保存呢
.... 添加 修改刪除 ,這是3個方法啊,
比如說 刪除方法, 你要獲取到datagridview 要刪除數據的ID, 然後根據ID 來當做sql語句查詢的where 條件, 執行修改的SQL語句 來刪除資料庫中的數據
保存 其實也就是修改。
H. 我在datagridview中添加修改之後 怎樣直接保存到資料庫
用SqlCommandBuilder 類
發送反饋
自動生成單表命令,用於將對 DataSet 所做的更改與關聯的 SQL Server 資料庫的更改相協調。 無法繼承此類。
命名空間: System.Data.SqlClient
程序集: System.Data(在 System.Data.dll 中)
語法
--------------------------------------------------------------------------------
VBC#C++F#JScript以帶有顏色區分的格式查看復制到剪貼板列印Public NotInheritable Class SqlCommandBuilder _
Inherits DbCommandBuilder
Public NotInheritable Class SqlCommandBuilder _
Inherits DbCommandBuilder
public sealed class SqlCommandBuilder : DbCommandBuilder
public sealed class SqlCommandBuilder : DbCommandBuilder
public ref class SqlCommandBuilder sealed : public DbCommandBuilder
public ref class SqlCommandBuilder sealed : public DbCommandBuilder
[<SealedAttribute>]
type SqlCommandBuilder =
class
inherit DbCommandBuilder
end
[<SealedAttribute>]
type SqlCommandBuilder =
class
inherit DbCommandBuilder
end
備注
--------------------------------------------------------------------------------
SqlDataAdapter 不會自動生成實現 DataSet 的更改與關聯的 SQL Server 實例之間的協調所需的 Transact-SQL 語句。 但是,如果設置了 SqlDataAdapter 的 SelectCommand 屬性,則可以創建一個 SqlCommandBuilder 對象來自動生成用於單表更新的 Transact-SQL 語句。 然後, SqlCommandBuilder 將生成其他任何未設置的 Transact-SQL 語句。
每當設置了 DataAdapter 屬性, SqlCommandBuilder 就將其本身注冊為 RowUpdating 事件的偵聽器。 一次只能將一個 SqlDataAdapter 與一個 SqlCommandBuilder 對象(或相反)互相關聯。
為了生成 INSERT、UPDATE 或 DELETE 語句, SqlCommandBuilder 會自動使用 SelectCommand 屬性來檢索所需的元數據集。 如果在檢索到元數據後(例如在第一次更新後)更改 SelectCommand,則應調用 RefreshSchema 方法來更新元數據。
SelectCommand 還必須至少返回一個主鍵列或唯一的列。 如果什麼都沒有返回,就會產生 InvalidOperation 異常,不生成命令。
SqlCommandBuilder 還使用由 SelectCommand 引用的 Connection、 CommandTimeout 和 Transaction 屬性。 如果修改了這些屬性中的一個或多個,或者替換了 SelectCommand 本身,用戶則應調用 RefreshSchema。 否則, InsertCommand、 UpdateCommand 和 DeleteCommand 屬性都保留它們以前的值。
如果調用 Dispose,則會解除 SqlCommandBuilder 與 SqlDataAdapter 的關聯,並且不再使用生成的命令。
例子
public static DataSet SelectSqlRows(string connectionString,
string queryString, string tableName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(queryString, connection);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
connection.Open();
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, tableName);
//code to modify data in DataSet here
builder.GetUpdateCommand();
//Without the SqlCommandBuilder this line would fail
adapter.Update(dataSet, tableName);
return dataSet;
}
}