C#“System.Data.SqlClient.SqlException”类型的未经处理的异常出现在 System.Data.dll中

guoqinfavour 2014-01-12 12:16:55
代码如下,求教
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)
{

}


}
}
...全文
1648 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Regan-lin 2014-01-12
  • 打赏
  • 举报
回复
断点,调试,把错误的代码贴上来!
bdmh 2014-01-12
  • 打赏
  • 举报
回复
自己调试,先确定那条代码报错,再检查sql语句是否正确,特别是insert,字段要对应
wind_cloud2011 2014-01-12
  • 打赏
  • 举报
回复
Sql a = new Sql(); 看代码内容va.DJ()是应该返回:"server=.;database=数据库;uid=sa;pwd=sa"; 调试时你看一下,可能问题出在这里,如果不是,直接用下面这样试一下 SqlConnection con= new SqlConnection("server=.;database=数据库;uid=sa;pwd=sa");
  • 打赏
  • 举报
回复
Sql 是个神马。
  • 打赏
  • 举报
回复
Sql类的构造方法的代码贴来看下
guoqinfavour 2014-01-12
  • 打赏
  • 举报
回复

调试,就到这一步就执行不了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧