<>实例:光盘\TM\sl\15\8的问题(已解决)
发表在C#图书答疑
2008-12-09
是否精华
是
否
版块置顶:
是
否
大家好!
我是C#初学者,在<<C#从入门到精通>>一书的光盘\TM\sl\15\8实例中,我仿其做了一个小程序,和实例差不多,只是用的是ACCESS数据库。前面显示数据都没有出错,当我点修改的时候就出错了,各位知道怎么解决吗?我对C#的数据库访问技术和流程还不是很明白。
图片出错如下:
[img src=http://hiphotos.baidu.com/xinshang%5Fjijie/pic/item/4b46534a9346093e08f7ef00.jpg/img]
代码如下:
开始的声明部分
OleDbConnection conn;
DataSet ds_select;
OleDbDataAdapter sda_select;
//----------------------------------------------------------------
窗体加载时
private void Form1_Load(object sender, EventArgs e)
{
try
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=C:\Documents and Settings\Administrator\桌面\# xd.mdb";
conn = new OleDbConnection(strConnection);
conn.Open();
if (conn.State == ConnectionState.Open)
{
label1.Text = "数据库已经连接";
}
}
catch
{
label1.Text = ("连接数据库失败");
}
}
//-------------------------------------------------------------------------------------------
加载数据时(运行成功)
private void button6_Click(object sender, EventArgs e)
{
try
{
OleDbCommand cmd_select = new OleDbCommand("select ID,SiteName,SiteUrl,Email from FriendLinks", conn);
OleDbDataAdapter sda_select = new OleDbDataAdapter();//创建一个OleDbDataAdapter对象
sda_select.SelectCommand = cmd_select;
DataSet ds_select = new DataSet();//创建一个DataSet对象
sda_select.Fill(ds_select, "bs");
dataGridView1.DataSource = ds_select.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//----------------------------------------------------------------------------
单击数据时(运行成功)
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox3.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox4.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox5.Text = dataGridView1.SelectedCells[2].Value.ToString();
textBox6.Text = dataGridView1.SelectedCells[3].Value.ToString();
}
//------------------------------------------------------------------------------
执行修改数据时(失败)
private void button7_Click(object sender, EventArgs e)
{
try
{
DataTable dt = ds_select.Tables["bs"];
sda_select.FillSchema(dt, SchemaType.Mapped);
DataRow dr = dt.Rows.Find(textBox3.Text);
//设置DataRow中的值
dr["SiteName"] = textBox4.Text.Trim();
dr["SiteUrl"] = textBox5.Text.Trim();
dr["Email"] = textBox6.Text.Trim();
//实例化一个OledbCommandBuilder
OleDbCommandBuilder cmdbuider = new OleDbCommandBuilder(sda_select);
//调用Update方法将DataTable更新到数据库中
sda_select.Update(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//--------------------------------------解决如下:---------------------------------------------
把在“加载数据”时的
OleDbCommand cmd_select = new OleDbCommand();
OleDbDataAdapter sda_select = new OleDbDataAdapter();
改为:
cmd_select = new OleDbCommand();
sda_select = new OleDbDataAdapter();
(当然,在声明部分要加上对这两个对象的定义。莫非我在上面的出错,就是因为创建的是“局部对象”?)
我是C#初学者,在<<C#从入门到精通>>一书的光盘\TM\sl\15\8实例中,我仿其做了一个小程序,和实例差不多,只是用的是ACCESS数据库。前面显示数据都没有出错,当我点修改的时候就出错了,各位知道怎么解决吗?我对C#的数据库访问技术和流程还不是很明白。
图片出错如下:
[img src=http://hiphotos.baidu.com/xinshang%5Fjijie/pic/item/4b46534a9346093e08f7ef00.jpg/img]
代码如下:
开始的声明部分
OleDbConnection conn;
DataSet ds_select;
OleDbDataAdapter sda_select;
//----------------------------------------------------------------
窗体加载时
private void Form1_Load(object sender, EventArgs e)
{
try
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=C:\Documents and Settings\Administrator\桌面\# xd.mdb";
conn = new OleDbConnection(strConnection);
conn.Open();
if (conn.State == ConnectionState.Open)
{
label1.Text = "数据库已经连接";
}
}
catch
{
label1.Text = ("连接数据库失败");
}
}
//-------------------------------------------------------------------------------------------
加载数据时(运行成功)
private void button6_Click(object sender, EventArgs e)
{
try
{
OleDbCommand cmd_select = new OleDbCommand("select ID,SiteName,SiteUrl,Email from FriendLinks", conn);
OleDbDataAdapter sda_select = new OleDbDataAdapter();//创建一个OleDbDataAdapter对象
sda_select.SelectCommand = cmd_select;
DataSet ds_select = new DataSet();//创建一个DataSet对象
sda_select.Fill(ds_select, "bs");
dataGridView1.DataSource = ds_select.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//----------------------------------------------------------------------------
单击数据时(运行成功)
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox3.Text = dataGridView1.SelectedCells[0].Value.ToString();
textBox4.Text = dataGridView1.SelectedCells[1].Value.ToString();
textBox5.Text = dataGridView1.SelectedCells[2].Value.ToString();
textBox6.Text = dataGridView1.SelectedCells[3].Value.ToString();
}
//------------------------------------------------------------------------------
执行修改数据时(失败)
private void button7_Click(object sender, EventArgs e)
{
try
{
DataTable dt = ds_select.Tables["bs"];
sda_select.FillSchema(dt, SchemaType.Mapped);
DataRow dr = dt.Rows.Find(textBox3.Text);
//设置DataRow中的值
dr["SiteName"] = textBox4.Text.Trim();
dr["SiteUrl"] = textBox5.Text.Trim();
dr["Email"] = textBox6.Text.Trim();
//实例化一个OledbCommandBuilder
OleDbCommandBuilder cmdbuider = new OleDbCommandBuilder(sda_select);
//调用Update方法将DataTable更新到数据库中
sda_select.Update(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//--------------------------------------解决如下:---------------------------------------------
把在“加载数据”时的
OleDbCommand cmd_select = new OleDbCommand();
OleDbDataAdapter sda_select = new OleDbDataAdapter();
改为:
cmd_select = new OleDbCommand();
sda_select = new OleDbDataAdapter();
(当然,在声明部分要加上对这两个对象的定义。莫非我在上面的出错,就是因为创建的是“局部对象”?)