dateGridView将数据从数据库中掉入内存,比如
OracleConnection conn = new OracleConnection(Properties.Settings.Default.ConnectionString);
OracleDataAdapter adapter = new OracleDataAdapter("select * from test", conn);
DataTable dt = new DataTable();
this.bindingSource1.DataSource = dt;
adapter.Fill(dt);请问此时如何对表进行update操作(update完后页面要刷新),然后再将update的数据回写到数据库怎么做
OracleConnection conn = new OracleConnection(Properties.Settings.Default.ConnectionString);
OracleDataAdapter adapter = new OracleDataAdapter("select * from test", conn);
DataTable dt = new DataTable();
this.bindingSource1.DataSource = dt;
adapter.Fill(dt);请问此时如何对表进行update操作(update完后页面要刷新),然后再将update的数据回写到数据库怎么做
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form
{
private DataGridView dataGridView1 = new DataGridView();
private BindingSource bindingSource1 = new BindingSource();
private SqlDataAdapter dataAdapter = new SqlDataAdapter();
private Button reloadButton = new Button();
private Button submitButton = new Button(); [STAThreadAttribute()]
public static void Main()
{
Application.Run(new Form1());
} // Initialize the form.
public Form1()
{
dataGridView1.Dock = DockStyle.Fill; reloadButton.Text = "reload";
submitButton.Text = "submit";
reloadButton.Click += new System.EventHandler(reloadButton_Click);
submitButton.Click += new System.EventHandler(submitButton_Click); FlowLayoutPanel panel = new FlowLayoutPanel();
panel.Dock = DockStyle.Top;
panel.AutoSize = true;
panel.Controls.AddRange(new Control[] { reloadButton, submitButton }); this.Controls.AddRange(new Control[] { dataGridView1, panel });
this.Load += new System.EventHandler(Form1_Load);
this.Text = "DataGridView databinding and updating demo";
} private void Form1_Load(object sender, System.EventArgs e)
{
// Bind the DataGridView to the BindingSource
// and load the data from the database.
dataGridView1.DataSource = bindingSource1;
GetData("select * from Customers");
} private void reloadButton_Click(object sender, System.EventArgs e)
{
// Reload the data from the database.
GetData(dataAdapter.SelectCommand.CommandText);
} private void submitButton_Click(object sender, System.EventArgs e)
{
// Update the database with the user's changes.
dataAdapter.Update((DataTable)bindingSource1.DataSource);
} private void GetData(string selectCommand)
{
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
String connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost"; // Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(selectCommand, connectionString); // Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); // Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table; // Resize the DataGridView columns to fit the newly loaded content.
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (SqlException)
{
MessageBox.Show("To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system.");
}
}}
如果此时(Fill完)update ,更改的数据库的数据 还是内存的数据啊数据库中的数据..
using System.Data.SqlClient;namespace DataGridViewUpdateToDataBase
{
public partial class Form1 : Form
{
private SqlDataAdapter sda = new SqlDataAdapter();
private BindingSource bs = new BindingSource();
public Form1()
{
InitializeComponent();
} //加载
private void btnLoad_Click(object sender, EventArgs e)
{
DataBind();
} //数据绑定
private void DataBind()
{
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
sda = new SqlDataAdapter("select * from studentDetails", con);
SqlCommandBuilder buider = new SqlCommandBuilder(sda);
DataSet ds = new DataSet();
sda.Fill(ds, "student");
bs .DataSource =ds.Tables ["student"];
this.dataGridView1.DataSource = bs;
} //更新
private void btnUpdate_Click(object sender, EventArgs e)
{
sda.Update((DataTable)bs.DataSource);
}
}
}
string queryString, string tableName)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(queryString, connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter); connection.Open(); DataSet customers = new DataSet();
adapter.Fill(customers); //code to modify data in dataset here
//注意这里你对DataTable中的值进行update,insert,delete .. adapter.Update(customers, tableName); return customers;
}
}以上是OleDb的,你换成相应SqlDataAdapter,SqlCommandBuilder等一样用...