C#“System.Data.SqlClient.SqlException”类型的未经处理的异常出现在 System.Data.dll中
代码如下,求教
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 图书管理系统
{
public partial class 图书信息 : Form
{
string dataMember;
public 图书信息()
{
InitializeComponent();
dataMember = "图书表";
BuildData();
}
private void BuildData()
{
Sql a = new Sql();
string sqlStr = "select *from 图书表";
SqlConnection con = new SqlConnection(a.DJ());
SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, con);
DataSet ds = new DataSet();
adapter.Fill(ds, dataMember);
this.dataGridView1.DataSource = ds.Tables[dataMember];
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void 图书信息_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“book_manage_systemDataSet.图书表”中。您可以根据需要移动或移除它。
this.图书表TableAdapter.Fill(this.book_manage_systemDataSet.图书表);
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "")
{
MessageBox.Show("输入不能为空,重新输入");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
}
else
{
try
{
Sql a = new Sql();
SqlConnection con = new SqlConnection(a.DJ());
con.Open();
string cmdstring = "insert into 图书表 values('" + this.textBox1.Text + "','" + this.textBox2.Text +
"', '" + this.textBox3.Text + "', '" + this.textBox4.Text + "', '" + this.maskedTextBox1.Text + "', '" + this.textBox6.Text + "', '" + this.textBox7.Text + "', '" + this.textBox8.Text + "')";
SqlCommand cmd = new SqlCommand(cmdstring, con);
cmd.ExecuteNonQuery();
BuildData();
con.Close();
}
catch
{
MessageBox.Show("请按照格式真确输入");
}
}
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("输入不能为空,重新输入");
textBox1.Text = "";
}
else
{
try
{
Sql a = new Sql();
SqlConnection con = new SqlConnection(a.DJ());
con.Open();
string cmdstring = "delete from 图书表 where 图书编号='" + this.textBox1.Text + "'";
SqlCommand cmd = new SqlCommand(cmdstring, con);
cmd.ExecuteNonQuery();
BuildData();
con.Close();
}
catch
{
MessageBox.Show("请真确输入图书编号");
}
}
}
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}