首页上一页 1 下一页尾页 1 条记录 1/1页
帮我看看
发表在C#图书答疑
2010-09-27
是否精华
是
否
版块置顶:
是
否
try
{
SqlConnection conn=new SqlConnection ();
conn.ConnectionString =" Server=PC-200907140942\\SA;uid=sa;pwd=sa; database=db_11 ";
conn.Open ();
string SqlIns = "Insert into tb_11 values('" + txtId.Text + "','" + txtName.Text + "' ,'" + txtSex.Text + "','" + txtTell.Text + "')";
//string SqlIns = "Insert into tb_11 values('" + txtId.Text + "','" +txtName.Text + "' ) ";
SqlCommand command=new SqlCommand ();
command .CommandText =SqlIns ;
command .Connection =conn;
command .ExecuteNonQuery ();
MessageBox .Show ("数据添加成功!");
SqlDataAdapter AdapterSelect=new SqlDataAdapter ("select * from tb_11",conn );
DataTable dt=new DataTable ();
AdapterSelect .Fill (dt );
//dataGridView1.DataSource = dt.DefaultView();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
这个代码有没有错误,添加不成功。如果dataGridView1.DataSource = dt.DefaultView();这个没有加注释就会出现错误提示,错误 “ Non-invocable member 'System.Data.DataTable.DefaultView' cannot be used like a method. ”去掉就可以,但运行结果显示不出。提示“仅当使用了列的列表,并且IDENTITY_INSERT为ON时,才能在表'tb_11'中为标识列指定显式值。”这个,在C#程序设计标准教程书本P272的例子,我只不过把里面的表改了一下。谢谢!!!
{
SqlConnection conn=new SqlConnection ();
conn.ConnectionString =" Server=PC-200907140942\\SA;uid=sa;pwd=sa; database=db_11 ";
conn.Open ();
string SqlIns = "Insert into tb_11 values('" + txtId.Text + "','" + txtName.Text + "' ,'" + txtSex.Text + "','" + txtTell.Text + "')";
//string SqlIns = "Insert into tb_11 values('" + txtId.Text + "','" +txtName.Text + "' ) ";
SqlCommand command=new SqlCommand ();
command .CommandText =SqlIns ;
command .Connection =conn;
command .ExecuteNonQuery ();
MessageBox .Show ("数据添加成功!");
SqlDataAdapter AdapterSelect=new SqlDataAdapter ("select * from tb_11",conn );
DataTable dt=new DataTable ();
AdapterSelect .Fill (dt );
//dataGridView1.DataSource = dt.DefaultView();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
这个代码有没有错误,添加不成功。如果dataGridView1.DataSource = dt.DefaultView();这个没有加注释就会出现错误提示,错误 “ Non-invocable member 'System.Data.DataTable.DefaultView' cannot be used like a method. ”去掉就可以,但运行结果显示不出。提示“仅当使用了列的列表,并且IDENTITY_INSERT为ON时,才能在表'tb_11'中为标识列指定显式值。”这个,在C#程序设计标准教程书本P272的例子,我只不过把里面的表改了一下。谢谢!!!