我打算在网页上实现这样一个功能,就是能够在网页上向数据库添加信息,可是运行的时候报错,代码和错误如下:
protected void Button1_Click(object sender, EventArgs e)
{
string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["WdbConnectionString"].ToString();//获取web.config中的数据库连接 string sqlstr = "insert into T_Student(StudentID,Name,Age,ChineseMark,MathMark,EnglishMark) values('";
sqlstr = sqlstr + TextBox1.Text + "','" + TextBox2.Text + "','"+ TextBox3.Text + "',' "+ TextBox4.Text +"','"+TextBox5 +"','"+TextBox6 +"')";//构造插入sql语句 using (SqlConnection connection = new SqlConnection(ConnStr))
{
SqlCommand command = new SqlCommand(sqlstr, connection);
connection.Open();//打开数据库连接
command.ExecuteNonQuery();//执行数据库命令,插入记录
// Call Close when done reading.
connection.Close();//关闭数据库
} Response.Redirect("search2.aspx");//重定向到default.aspx页面。 }在运行的时候,就在红色字体的部分表错,说T_Student无效,可是这个是我需要插入的表的表名,怎么会无效呢?请各位帮忙看一看。谢谢!!
protected void Button1_Click(object sender, EventArgs e)
{
string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["WdbConnectionString"].ToString();//获取web.config中的数据库连接 string sqlstr = "insert into T_Student(StudentID,Name,Age,ChineseMark,MathMark,EnglishMark) values('";
sqlstr = sqlstr + TextBox1.Text + "','" + TextBox2.Text + "','"+ TextBox3.Text + "',' "+ TextBox4.Text +"','"+TextBox5 +"','"+TextBox6 +"')";//构造插入sql语句 using (SqlConnection connection = new SqlConnection(ConnStr))
{
SqlCommand command = new SqlCommand(sqlstr, connection);
connection.Open();//打开数据库连接
command.ExecuteNonQuery();//执行数据库命令,插入记录
// Call Close when done reading.
connection.Close();//关闭数据库
} Response.Redirect("search2.aspx");//重定向到default.aspx页面。 }在运行的时候,就在红色字体的部分表错,说T_Student无效,可是这个是我需要插入的表的表名,怎么会无效呢?请各位帮忙看一看。谢谢!!
现在出现的是这个问题了!!不是上面那个了!!但是报错的地方仍然是:command.ExecuteNonQuery();//执行数据库命令,插入记录
这个地方
改完以后的代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["WdbConnectionString"].ToString();//获取web.config中的数据库连接 string sqlstr = "insert into T_Student(Name,Age,ChineseMark,MathMark,EnglishMark) values('";
sqlstr = sqlstr + TextBox2.Text + "','" + TextBox3.Text + "','"+ TextBox4.Text + "',' "+ TextBox5.Text +"','"+TextBox6 +"')";//构造插入sql语句 using (SqlConnection connection = new SqlConnection(ConnStr))
{
SqlCommand command = new SqlCommand(sqlstr, connection);
connection.Open();//打开数据库连接
command.ExecuteNonQuery();//执行数据库命令,插入记录 // Call Close when done reading.
connection.Close();//关闭数据库
} Response.Redirect("search2.aspx");//重定向到default.aspx页面。 }
谢谢啦!!
你的意思是说,我这里有问题吗?
这里: sqlstr = sqlstr + TextBox2.Text + "','" + TextBox3.Text + "','"+ TextBox4.Text + "',' "+ TextBox5.Text +"','"+TextBox6 +"')"