简单问题,求指教!datagridviw删除和修改的两个问题!
问题1:当我点击删除那个button时,结果总是显示的删除失败,为什么啊?大家帮忙改改吧以下是我的代码:
private void btndel_Click(object sender, EventArgs e)
{
string bno =BookInfo.CurrentRow.Cells[0].Value.ToString();
string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bookmanage.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connStr);
string sql = String.Format("delete from bkinfo where Bno='"+bno+"'");
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("删除图书成功", "删除成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("删除图书失败", "删除失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
conn.Close();
}
}
问题2:点击修改后,错位地方为:dataAdapter.Update(dataSet,"bkinfo");
报错为“未将对象引用设置到对象的实例”。该怎么修改?
以下是我整个窗体的代码:
public partial class BookAlter : Form
{
SqlDataAdapter dataAdapter;
DataSet dataSet;
public BookAlter()
{
InitializeComponent();
}
private void btnlook_Click(object sender, EventArgs e)
{
string bookname = txtName.Text;
string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bookmanage.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connStr);
string sql = String.Format("select * from bkinfo where Bname like'%{0}%'", bookname);
try
{
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, conn);
DataSet dataSet = new DataSet("Bkinfo");
dataAdapter.Fill(dataSet);
BookInfo.Columns[0].DataPropertyName = "Bno";
BookInfo.Columns[1].DataPropertyName = "Bname";
BookInfo.Columns[2].DataPropertyName = "Writer";
BookInfo.Columns[3].DataPropertyName = "Btype";
BookInfo.Columns[4].DataPropertyName = "Bpress";
BookInfo.Columns[5].DataPropertyName = "Bnumber";
BookInfo.Columns[6].DataPropertyName = "Bprice";
BookInfo.DataSource = dataSet.Tables[0];//[0]代表数据集的第一个表
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void btnalter_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder=new SqlCommandBuilder(dataAdapter);
dataAdapter.Update(dataSet,"bkinfo");
}
private void btnout_Click(object sender, EventArgs e)
{
this.Close();
}
}
} --------------------编程问答-------------------- 求高手帮忙啊…… --------------------编程问答-------------------- string sql = String.Format("delete bkinfo where Bno='"+bno+"'"); --------------------编程问答-------------------- dataAdapter 和 dataSet 没有实例化
--------------------编程问答-------------------- 那该怎么改呢? --------------------编程问答-------------------- 貌似不是这个问题,对了,其实选中的那条已经删除了,可现在不知道为什么显示的却是删除失败。为啥呢? --------------------编程问答-------------------- 我试着实例化后,又出现问题了,Update 无法找到 TableMapping['bkinfo'] 或 DataTable“bkinfo”。 --------------------编程问答--------------------
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery(); 这句话去掉
int count = cmd.ExecuteNonQuery(); --------------------编程问答--------------------
private void btnalter_Click(object sender, EventArgs e)
{
string connStr = @"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bookmanage.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connStr);
string sql ="select * from bkinfo";
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, conn);
DataSet dataSet = new DataSet("Bkinfo");
dataAdapter.Fill(dataSet);
SqlCommandBuilder builder=new SqlCommandBuilder(dataAdapter);
dataAdapter.Update(dataSet,"bkinfo");
}
--------------------编程问答--------------------
+1
补充:.NET技术 , C#