代码如下
public class biaodan : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string yyy = context.Request["name"];
//int age = Convert.ToInt32(context.Request["age"]);
string ppp = context.Request["email"];
using (SqlConnection conn = new SqlConnection(
"Data Source=.;Initial Catalog=students;User ID=sa;Password=a6811065a "))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand()) //SqlCommand代表向数据库发出指令的这个个对象
{
cmd.CommandText = "Insert into Table_1(name,email) values(" + yyy + "," + ppp + ")";
cmd.ExecuteNonQuery();
}
}
}
当我向yyy和ppp传入值asd和asd时,总出现
求大神解答一下,怎么才能把yyy和ppp参数的值传入数据库。
很明显Table_1表没有列名为 asd 的列啊,
还有你的Sql也写错了啊
cmd.CommandText = "Insert into Table_1(name,email) values(" + yyy + "," + ppp + ")";
改
cmd.CommandText = "Insert into Table_1(name,email) values('" + yyy + "','" + ppp + "')";
要在字符串上面加上 ' '
谢谢,加个单引号就好了
cmd.CommandText=string.Format("Insert into Table1(name,email) values('{0}','{1}',yyyy,ppp)"); //注意这里是t-sql 插入类型字符串是'' 除非你特别熟悉,不然不要通过+号拼接
你的截图没有显示出来?
谢谢,用了一下,还是报错