得到错误,同时通过文本框的数据存储在SQL Server 2005中 [英] Getting error while store data in sql server 2005 through textbox

查看:157
本文介绍了得到错误,同时通过文本框的数据存储在SQL Server 2005中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过文本框和按钮存储在SQL Server 2005数据(约1500字)。我用这code。

I am storing data (approx. 1500 words) in SQL server 2005 through textbox and button. I am using this code.

protected void Button1_Click(object sender, EventArgs e)
{
    conn.Open();
    String query = String.Format("insert into try (data,sno) values ('{0}',22)",TextBox1.Text);
    SqlCommand cmd = new SqlCommand(query, conn);
    cmd.ExecuteNonQuery();
    Label1.Text = "submitted";
    conn.Close();
}

我有数据类型'字符(4000)

问题是,当我存储1段(约1500字),它成功地存储。但是,当我保存的另一段(约1500字),它给我的错误。

Problem is that, when I store 1st paragraph (approx 1500 words), it stored successfully. But when I stored another paragraph (approx 1500 words), it show me the error.

附近的。之后,闭合的引号的语法不正确
  字符串,22)'。

"Incorrect syntax near 's'. Unclosed quotation mark after the character string ',22)'."

这是什么问题?

推荐答案

使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx\">Parameters

String query = "insert into try (data,sno) values (@data,22)"; 
SqlCommand cmd = new SqlCommand(query, conn); 
cmd.Parameters.AddWithValue("@data", TextBox1.text);
cmd.ExecuteNonQuery(); 

在这种方式,你不必担心你的文本单引号和,最重要的事情,你避免的 SQL注入攻击的攻击

In this way you don't need to worry about the presence of single quotes in your text and, the most important thing, you avoid SqlInjection Attacks

这篇关于得到错误,同时通过文本框的数据存储在SQL Server 2005中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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