1、(1)此处只允许使用常量、表达式或变量。不允许使用列名。

错误原因:

string sql = "insert into category(name) values(" + caName + ")";//错误,应该在caName上加单引号
string sql = "insert into category(name) values('" + caName + "')"; //修改后

 (2)新建网站,新建类库,但是打开后类库“无法生成”。

解决:打开----->网站------>选择web网站,然后在解决方案上增加“现有项目" ,把DAL类库和BLL类库重新增加进来。

(3)有关using System.Configuration;

当引用Configuration时,需要在网站项目上添加引用 ”.net"---"System.Configuration",否则在编程时有些东西看不到。

2  通过配置文件来获取连接字符串

(1)在web.config中,添加下面代码

  <connectionStrings>
    <add name ="connStr" connectionString="server=.;database = newsSystem;uid=sa;pwd=sa"/>
  </connectionStrings>

(2)在DAL(数据访问层 或 要使用连接字符串的项目中) 添加引用 ”.net"---"System.Configuration",

还要使用using System.Configuration;

(3)通过下面代码即可访问:

string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
conn = new SqlConnection(connStr);

3   sql注入,拼接字符串容易被sql注入,如

string sql = "insert into category(name) values('" + caName + "')";

防止sql注入,可以使用待参数的sql

 

 

相关文章:

  • 2019-10-23
  • 2022-01-07
  • 2022-01-15
  • 2021-11-15
  • 2021-10-08
猜你喜欢
  • 2021-04-02
  • 2022-01-20
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2019-03-26
相关资源
相似解决方案