没有定义Key键了, mycustomers(.net) ( ) 信誉:100 Blog 2006-12-11 16:15:52 得分: 0
不定义主键还好,如果定义主键.直接抱"配置数据是发生错误" 哎~~~~~~~~~
----------------- 说不定是你定义的KEY有重复!
如删除记录:该怎么样写 int i = this.dataGridView1.CurrentCell.RowIndex; this.dataSet11.Tables["customers"].Rows[i].BeginEdit(); this.dataSet11.Tables["customers"].Rows[i].Delete(); this.dataSet11.Tables["customers"].Rows[i].EndEdit(); this.sqlDataAdapter1.Update(dataSet11.Tables["customers"]); MessageBox.Show("删除成功!");
sql没用过,oracle的这样写 DataTable dt = BindingSource.DataSource as DataTable; OracleConnection conn = new OracleConnection(); conn.ConnectionString = Properties.Settings.Default.Setting; OracleDataAdapter adapter = new OracleDataAdapter("select * from customers", conn); OracleCommandBuilder builder = new OracleCommandBuilder(); builder.ConflictOption = ConflictOption.OverwriteChanges; builder.DataAdapter = adapter; adapter.Update(dt);
下面的完整代码示例提供的按钮用于从数据库重新加载数据和向数据库提交更改。using System; 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."); } }}
下面的完整代码示例提供的按钮用于从数据库重新加载数据和向数据库提交更改。using System; 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."); } }}
*****************************************************************************
欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
不定义主键还好,如果定义主键.直接抱"配置数据是发生错误"
哎~~~~~~~~~
-----------------
说不定是你定义的KEY有重复!
int i = this.dataGridView1.CurrentCell.RowIndex;
this.dataSet11.Tables["customers"].Rows[i].BeginEdit();
this.dataSet11.Tables["customers"].Rows[i].Delete();
this.dataSet11.Tables["customers"].Rows[i].EndEdit();
this.sqlDataAdapter1.Update(dataSet11.Tables["customers"]);
MessageBox.Show("删除成功!");
DataTable dt = BindingSource.DataSource as DataTable;
OracleConnection conn = new OracleConnection();
conn.ConnectionString = Properties.Settings.Default.Setting;
OracleDataAdapter adapter = new OracleDataAdapter("select * from customers", conn);
OracleCommandBuilder builder = new OracleCommandBuilder();
builder.ConflictOption = ConflictOption.OverwriteChanges;
builder.DataAdapter = adapter;
adapter.Update(dt);
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.");
}
}}
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.");
}
}}
1.在ASP.Net以外的项目中使用,直接更改对应的数据集表中的主键,具体方法为在有键的行上右击鼠标,选删除。
2.在ASP.Net上没用用数据集的,在选择表示,钩选“唯一行”选项,可以正常通过,且并不影响最终结果(不知道为什么会不影响,谁会也帮我回答下)