Inser Into Table问题 我在程序中写SQL语句,直接取TextBox上的值插入数据库,如下:strsql = "Insert into table ( name ) values ( '" + textBox.text + "' )";一般情况下没有问题,但是如果txt框中的内容是有符号'的话,那就出错了,应该怎么处理?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1。如果单引号必须插入数据库的话可以先将单引号替换成一个不常用的字符比如"|"或"^"strsql = "Insert into table ( name ) values ( '" + textBox.text.Replace("'","|") + "' )";取出时在替换过来2。不允许输入单引号 用参数方法OleDbConnection conn = new OleDbConnection();conn.ConnectionString = "[数据库联接串]";conn.Open();OleDbCommand comm = new SqlCommand();comm.Connection = conn;comm.CommandText = "Insert into table ( name ) values (@name)";comm.Parameters.Add("@name", SqlDbType.VarChar);comm.Parameters["@name"].Value = textBox.text;try{ comm.ExecuteNonQuery();}cache{ throw;}finally{ conn.Close(); conn.Dispose();} string kk=textBox.text ;kk=kk.Replace("'","''");strsql = "Insert into table ( name ) values ( '" + kk+ "' )"; 拼SQL语句的时候,如果要输单引号就要一个变二个输入。 最好的就是 wlzbaby(没病真好)的解决方法了. fileupload上传大文件的问题 甘特图控件实现 html的Table控件问题 高手进! Gridview的问题 求SqlCommand.Parameters的用法! 我在做登陆页面时, 遇到如此问题,请教各位,在线等 大家帮忙给新域名取个网站名字! 如何实现:点击页面中的按钮,就可以保存该页面? 要修改GridView中的日期和星期颜色 急!ms的日历控件问题 Sql语句找错误!!!
strsql = "Insert into table ( name ) values ( '" + textBox.text.Replace("'","|") + "' )";
取出时在替换过来
2。不允许输入单引号
conn.ConnectionString = "[数据库联接串]";
conn.Open();OleDbCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "Insert into table ( name ) values (@name)";
comm.Parameters.Add("@name", SqlDbType.VarChar);
comm.Parameters["@name"].Value = textBox.text;try
{
comm.ExecuteNonQuery();
}
cache
{
throw;
}
finally
{ conn.Close();
conn.Dispose();
}
kk=kk.Replace("'","''");
strsql = "Insert into table ( name ) values ( '" + kk+ "' )";