在ASP.NET中插入项集合 [英] Inserting item collection in ASP.NET

查看:74
本文介绍了在ASP.NET中插入项集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入一些我作为项目保留的单词。我的项目是asp.net web项目。我想为每个单词生成id并保持4个字段为null。我的6个字段数据库中的单词表.d单词长度fr布尔权重。



当我运行项目时出现此错误。






'/'应用程序中的服务器错误。



无效的列名称'antarctic'。



描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.Data.SqlClient.SqlException:无效的列名'antarctic'。



来源错误:





第57行:SqlCommand cmd = con.CreateCommand();

第58行: cmd.CommandText =插入字值(+ id +,+ w +,+ 0 +,+ 0 +,+ 0 +,+ 0 +);

第59行:cmd.ExecuteNonQuery();

第60行:

第61行:}






 使用(con){
con.Open();

foreach var item 结果中){ // 这里试图插入其id和其他一些信息,但现在他们可以保持null(是的,允许为null)
id ++;
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = 插入字值(' + id + ',' + item.Word + ',' +0 + ',' +0+ ',' +0 + ',' +0+ ');

cmd.ExecuteNonQuery();

} con.Close();
}}





我的尝试:



我已经检查了我的数据库表,word字段定义为nvarchar(50),我认为应该没问题。

解决方案

代码你已经显示写得很差,这就是为什么你得到那个奇怪的错误。 南极是你传递的值之一,但因为你没有正确解析它认为它是一个列。



改变你的代码以拥有一个合适的sql语法和使用参数,以便您的数据库不打开sql注入。



例如:

 字符串 sql =   INSERT INTO word(id, word,field1,field2 ...)VALUES(@ id,@ word,0,0 ...)//确保在insert语句中指定字段名称。保护db更改。
cmd.Parameters。 AddWithValue(
@id ,id);
cmd.Parameters.AddWithValue(
@word ,item.Word);
cmd.ExecuteNonQuery();


I am trying to insert some words that i keep as an item .My project is asp.net web project.I would like to generate id for each word and keep 4 field null.There are 6 fields in my word table in database.id word length fr Boolean weight.

When I run the project I get this error.



Server Error in '/' Application.

Invalid column name 'antarctic'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'antarctic'.

Source Error:


Line 57: SqlCommand cmd= con.CreateCommand();
Line 58: cmd.CommandText= "insert into word values(" + id + "," + w + "," + 0 + "," + 0 + "," + 0 + "," + 0 + ")";
Line 59: cmd.ExecuteNonQuery();
Line 60:
Line 61: }



using (con){
        con.Open();

        foreach (var item in results) {//here trying to insert word its id and some other informations but for now they can stay null(yes,null allowed for them)
            id++;
            SqlCommand cmd= con.CreateCommand();
            cmd.CommandText= "insert into word values('"+id+"','"+item.Word+"','"+0+"','"+0+"','"+0+"','"+0+"')";

            cmd.ExecuteNonQuery();

        }  con.Close();
}   }



What I have tried:

I have checked my database table, word field isdefined as nvarchar(50),i think it should be ok.

解决方案

The code you have shown is poorly written which is why you are getting that weird error. "Antarctic" is one of the values you are passing but because you did not parse it correctly it thinks it is a column.

Change your code to have a proper sql syntax and also to use parameters so that your db is not open for sql injection.

For example:

String sql = "INSERT INTO word(id, word, field1, field2...) VALUES (@id, @word, 0, 0...) // make sure to specify field names in your insert statement.  Protects from db changes.
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@word", item.Word);
cmd.ExecuteNonQuery();


这篇关于在ASP.NET中插入项集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆