从文本框向SQL发送超过256个字符时出现ASP.NET错误 [英] ASP.NET error when sending more than 256 characters from a textbox to SQL
问题描述
我有一个ASP.NET网站,有许多文本框需要填写并写入SQL。我也有一个WinForm应用程序。
当我在winform中的文本框中写入超过256个字符并在SQL列(NVARCHAR(MAX))中INSERT或UPDATE时,没有问题,工作正常。
当我尝试在我的ASP网站上做同样的事情时,它给了我一个错误:
必须声明标量变量@
(是的,没有变量名!)。我没有在ASP中使用任何预定义的sql连接字符串,我只使用旧的学校方法:1个字符串与查询,一个ExecuteNonQuery()方法调用。我总是这样使用,在其他情况下工作正常。
我在谷歌搜索了很多东西,但没有成功。我想这是一些IIS限制或一些HTTP限制?!...
有人能告诉我一个简单的解决方案,能够上传和超过256的长文本chars?
非常感谢
Mika,你说得对,所以这是我的解决方案:
在我解决它之前,我使用了一个类来进行SQL插入和更新。我将2个args传递给该类:一个字符串,其值用字母ß分隔,以及数据库的名称,如下所示:TableData.WriteID(columnString,db)
columnString有这种字符串:data1ßdata2ßdata3
然后类将传入的字符串(Split('ß'))拆分成字符串数组然后我插入/更新了sql(使用NameValueCollection)。这不起作用。我从 Mohammed Hadi 那里得到了样本。这是一个非常酷的东西。
然后我回到了reeeeeal旧学校版本:
< pre lang =c#> string sql = INSERT INTO表(ID,用户名,评论,日期)VALUES(@ id,@ username,@ comment,@ date);
SqlCommand cmd = new SqlCommand(sql,myConnection); // 预定义myConnection
cmd.Parameters.Add( @ id ,id.Text);
cmd.Parameters.Add( @ username,username.Text);
cmd.Parameters.Add( @ comment,comment.Text);
cmd.Parameters.Add( @ date,date.Text);
cmd.ExecuteNonQuery();
这一个正在工作!
argh 。我有一个解决方案,但不知道它为什么有效。所以我必须在我的c#代码中使用cmd.parameters.add命令。
我用了一个类进行sql编写,我用逗号分隔整个字符串,并且该类将其纳入其字符串[] ......没关系。我以为我是老学校,但不是...... :)
Hi,
I have an ASP.NET website, that has many textboxes to be filled and to be written into SQL. I also have a WinForm application for this.
When I write more than 256 characters into a textbox in winform and INSERT or UPDATE it in SQL column (NVARCHAR(MAX)), have no problem, works fine.
When I try to do the same on my ASP net site, it gives me an error:
Must declare the scalar variable "@"
(yes, there is no variable name!). I don't use any predefinied sql connection string in ASP, i use only the old school method: 1 string with the query, one ExecuteNonQuery() method calling. I always use this way, and works fine in other cases.
I searched many things on google, but no success. I guess it is some IIS limitations or some HTTP limitation?!...
Can somebody tell me a simple solution to be able to upload and long text longer than 256 chars?
Many thanks
Mika, you're right, so here's my solution:
before I solved it, I used a class for SQL inserts and updates. I passed 2 args to that class: a string with the values separating them with letter ß, and the name of the database, something like this: TableData.WriteID(columnString, db)
the columnString had this kind of string: "data1ßdata2ßdata3"
then the class splitted the incoming string (Split('ß')) into a string array and then I inserted/updated the sql (using NameValueCollection). This was not working. I got the sample from Mohammed Hadi. It's a really cool stuff.
Then I got back to the reeeeeal old school version:
string sql = "INSERT INTO Table (ID,username,comment,date) VALUES (@id,@username,@comment,@date)"; SqlCommand cmd = new SqlCommand(sql, myConnection);//pre-definied myConnection cmd.Parameters.Add("@id", id.Text); cmd.Parameters.Add("@username", username.Text); cmd.Parameters.Add("@comment", comment.Text); cmd.Parameters.Add("@date", date.Text); cmd.ExecuteNonQuery();
And this one's working!
argh. I have a solution, but don't know why it works. so I have to use the cmd.parameters.add command in my c# code.
I used a class for sql writing, where I sent the whole string with comma separating, and the class got it into its string[]... nevermind. i thought i was old school, but not... :)
这篇关于从文本框向SQL发送超过256个字符时出现ASP.NET错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!