原谅我贴一大段代码.但是自己实在找不出原因了.希望有耐心的高手帮帮.出现的问题是 无法保存到数据库.设计界面上有datagridview1,raidobutton1,radiobutton2,bindingsource1,bindingnavigator1,
在bindingnavigator1上加了个保存BUTTON--------------------------------------------- class SqlClass
{
public SqlDataAdapter adaper = new SqlDataAdapter();
public DataTable table = new DataTable();
public SqlCommandBuilder buider = new SqlCommandBuilder();
public SqlConnection conn = new SqlConnection();
public SqlClass()
{ }
public SqlClass(string selectstring, string connstring)
{
try
{
conn = new SqlConnection(connstring);
adaper = new SqlDataAdapter(selectstring, conn);
buider = new SqlCommandBuilder(adaper);
adaper.DeleteCommand = buider.GetDeleteCommand();
adaper.UpdateCommand = buider.GetUpdateCommand();
adaper.InsertCommand = buider.GetUpdateCommand();
adaper.Fill(table); }
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
} }
}
-------------------------------------------------------------------------
namespace WindowsFormsApplication4
{
public partial class Form2 : Form
{
string table = "table1";
SqlClass sqlclass1 = new SqlClass();
public Form2()
{
InitializeComponent();
} private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
table = "table1";
}
else
{
table = "table2";
}
rebuild();
}
private void rebuild()
{
string connstring = Properties.Settings.Default.testConnectionString;
string selectstring1 = "select * from " + table;
sqlclass1 = new SqlClass(selectstring1, connstring); bindingSource1.DataSource = sqlclass1.table;
dataGridView1.DataSource = bindingSource1;
bindingNavigator1.BindingSource = bindingSource1;
} private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.Validate();
bindingSource1.EndEdit();
sqlclass1.adaper.Update(sqlclass1.table);
MessageBox.Show("成功");
}
catch
{
MessageBox.Show("保存错误");
}
}
}
}
-----------------------------------------------------------------------------------------------------
在bindingnavigator1上加了个保存BUTTON--------------------------------------------- class SqlClass
{
public SqlDataAdapter adaper = new SqlDataAdapter();
public DataTable table = new DataTable();
public SqlCommandBuilder buider = new SqlCommandBuilder();
public SqlConnection conn = new SqlConnection();
public SqlClass()
{ }
public SqlClass(string selectstring, string connstring)
{
try
{
conn = new SqlConnection(connstring);
adaper = new SqlDataAdapter(selectstring, conn);
buider = new SqlCommandBuilder(adaper);
adaper.DeleteCommand = buider.GetDeleteCommand();
adaper.UpdateCommand = buider.GetUpdateCommand();
adaper.InsertCommand = buider.GetUpdateCommand();
adaper.Fill(table); }
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
} }
}
-------------------------------------------------------------------------
namespace WindowsFormsApplication4
{
public partial class Form2 : Form
{
string table = "table1";
SqlClass sqlclass1 = new SqlClass();
public Form2()
{
InitializeComponent();
} private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
table = "table1";
}
else
{
table = "table2";
}
rebuild();
}
private void rebuild()
{
string connstring = Properties.Settings.Default.testConnectionString;
string selectstring1 = "select * from " + table;
sqlclass1 = new SqlClass(selectstring1, connstring); bindingSource1.DataSource = sqlclass1.table;
dataGridView1.DataSource = bindingSource1;
bindingNavigator1.BindingSource = bindingSource1;
} private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.Validate();
bindingSource1.EndEdit();
sqlclass1.adaper.Update(sqlclass1.table);
MessageBox.Show("成功");
}
catch
{
MessageBox.Show("保存错误");
}
}
}
}
-----------------------------------------------------------------------------------------------------
adaper.UpdateCommand = buider.GetUpdateCommand();
adaper.InsertCommand = buider.GetUpdateCommand();
你看一下这三个属性的只是什么?这样好像不行。
adaper.DeleteCommand = buider.GetDeleteCommand();
adaper.UpdateCommand = buider.GetUpdateCommand();
adaper.InsertCommand = buider.GetUpdateCommand();
GetUpdateCommand方法代码在哪?