首页上一页 1 下一页尾页 4 条记录 1/1页
数据库插入执行中出现异常,请检查数据库或数据库连接!
发表在ASP.NET图书答疑
2012-03-13
是否精华
是
否
版块置顶:
是
否
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
//创建使用Windows登陆的 SqlConnection对象
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
//创建SqlCommand对象
SqlCommand com = con.CreateCommand();
com.CommandText = "SELECT * FROM Employee";
//创建数据适配器对象
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.SelectCommand = com;
//填充DataSet
DataSet ds = new DataSet();
adpt.Fill(ds);
}
protected void btnInsert_Click(object sender, EventArgs e)
{
//组装插入SQL语句
string sql = string.Format("INSERT INTO [Employee] ([EmpName], [EmpAge], [EmpSex],[Info],[linkman],) VALUES (@EmpName, @EmpAge, @EmpSex, @Info,@linkman)", this.txtEmpName.Text, this.txtEmpAge.Text, this.ddlSex.SelectedItem.Text, this.newsinfo.Text, this.newsman.Text);
//实例化连接对象
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
//实例化命令对象
SqlCommand com = con.CreateCommand();
//设定插入操作的SQL语句
com.CommandText = sql;
//开启连接
//接受影响函数的整形变量
int result = 0;
try
{
con.Open();
//执行插入SQL语句
result = com.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
//关闭连接
con.Close();
}
//判断影响行数是否为1行 因为仅仅插入了一条记录
if (result == 1)
{
this.Response.Write("<script type='text/javascript' language='javascript'>alert('数据插入成功,请查看数据')</script> ");
}
else
{
this.Response.Write("<script type='text/javascript' language='javascript'>alert('数据插入执行中出现了异常,请检查数据库或数据库连接')</script> ");
}
//重新绑定GridView控件
this.BindGrid();
}
}
错误是:数据库插入执行中出现异常,请检查数据库或数据库连接!
可是当这句: string sql = string.Format("INSERT INTO [Employee] ([EmpName], [EmpAge], [EmpSex],[Info],[linkman],) VALUES (@EmpName, @EmpAge, @EmpSex, @Info,@linkman)", this.txtEmpName.Text, this.txtEmpAge.Text, this.ddlSex.SelectedItem.Text, this.newsinfo.Text, this.newsman.Text);
换成这样: string sql = string.Format("INSERT INTO [Employee] ([EmpName], [EmpAge], [EmpSex],[Info],[linkman],) VALUES ('@EmpName', '@EmpAge', '@EmpSex', '@Info,@linkman')", this.txtEmpName.Text, this.txtEmpAge.Text, this.ddlSex.SelectedItem.Text, this.newsinfo.Text, this.newsman.Text);
//实例化连接对象
居然能成功插入,而且数据库显示:'@EmpName', '@EmpAge', '@EmpSex', '@Info,@linkman'不是我想要的数据!!
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
//创建使用Windows登陆的 SqlConnection对象
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
//创建SqlCommand对象
SqlCommand com = con.CreateCommand();
com.CommandText = "SELECT * FROM Employee";
//创建数据适配器对象
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.SelectCommand = com;
//填充DataSet
DataSet ds = new DataSet();
adpt.Fill(ds);
}
protected void btnInsert_Click(object sender, EventArgs e)
{
//组装插入SQL语句
string sql = string.Format("INSERT INTO [Employee] ([EmpName], [EmpAge], [EmpSex],[Info],[linkman],) VALUES (@EmpName, @EmpAge, @EmpSex, @Info,@linkman)", this.txtEmpName.Text, this.txtEmpAge.Text, this.ddlSex.SelectedItem.Text, this.newsinfo.Text, this.newsman.Text);
//实例化连接对象
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
//实例化命令对象
SqlCommand com = con.CreateCommand();
//设定插入操作的SQL语句
com.CommandText = sql;
//开启连接
//接受影响函数的整形变量
int result = 0;
try
{
con.Open();
//执行插入SQL语句
result = com.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
//关闭连接
con.Close();
}
//判断影响行数是否为1行 因为仅仅插入了一条记录
if (result == 1)
{
this.Response.Write("<script type='text/javascript' language='javascript'>alert('数据插入成功,请查看数据')</script> ");
}
else
{
this.Response.Write("<script type='text/javascript' language='javascript'>alert('数据插入执行中出现了异常,请检查数据库或数据库连接')</script> ");
}
//重新绑定GridView控件
this.BindGrid();
}
}
错误是:数据库插入执行中出现异常,请检查数据库或数据库连接!
可是当这句: string sql = string.Format("INSERT INTO [Employee] ([EmpName], [EmpAge], [EmpSex],[Info],[linkman],) VALUES (@EmpName, @EmpAge, @EmpSex, @Info,@linkman)", this.txtEmpName.Text, this.txtEmpAge.Text, this.ddlSex.SelectedItem.Text, this.newsinfo.Text, this.newsman.Text);
换成这样: string sql = string.Format("INSERT INTO [Employee] ([EmpName], [EmpAge], [EmpSex],[Info],[linkman],) VALUES ('@EmpName', '@EmpAge', '@EmpSex', '@Info,@linkman')", this.txtEmpName.Text, this.txtEmpAge.Text, this.ddlSex.SelectedItem.Text, this.newsinfo.Text, this.newsman.Text);
//实例化连接对象
居然能成功插入,而且数据库显示:'@EmpName', '@EmpAge', '@EmpSex', '@Info,@linkman'不是我想要的数据!!