Database insert error: quot;string or binary data would be truncatedquot;(数据库插入错误:“字符串或二进制数据将被截断;)
问题描述
当我登录时,我将我的用户名存储在会话中.我的要求是我想将我的用户名存储在我的数据库中.这里我将它存储在 username1
中.输入用户名后,我可以使用 response.write()
打印它,并且打印效果很好.但是,当我将它存储在数据库中时,它会产生此错误:
以下是我的 ado.net 代码:
使用 (SqlConnection con =new SqlConnection("数据源=.;数据库=testdb1;集成安全=SSPI")) {con.Open();//SqlCommand cmd = new SqlCommand("delete from fileinfo where ID=" + Convert.ToInt32(Request.Params["one"]), con);string uname = (string) Session["fname"].ToString() + " " + Session["lname"].ToString();//Session["fname"].ToString()+" "+Session["lname"].ToString();//Response.Write(uname);//uname = "sri hari";uname = uname + " ";字符串 uname1 = uname;uname = uname.Trim();SqlCommand cmd = new SqlCommand("插入 qry_details 值('" + txt_query_name.Text + "','待批','" + txt_query_description.Text + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + qry + "','" + uname1 + "')", con);cmd.ExecuteScalar();}
检查qry_details表的长度,看它是否小于你发送到db的字符串?
基本上,例外情况表明您正在尝试比列长度更大的东西.
When I log in, I am storing my username in the session. My requirement is that I want to store my username in my database. Here I am storing it in username1
. When the username is entered, I can print it using response.write()
and it is printing perfectly. However, when I am storing it in the database it is producing this error:
**sqlException was unhandled by user code and exception at cmd.ExecuteScalar(); String or binary data would be truncated. The statement has been terminated.**
Following is my ado.net code:
using (SqlConnection con =
new SqlConnection("Data Source=.;database=testdb1;Integrated Security=SSPI")) {
con.Open();
// SqlCommand cmd = new SqlCommand("delete from fileinfo where ID=" + Convert.ToInt32(Request.Params["one"]), con);
string uname = (string) Session["fname"].ToString() + " " + Session["lname"].ToString(); //Session["fname"].ToString()+" "+Session["lname"].ToString();
// Response.Write(uname);
// uname = "sri hari";
uname = uname + " ";
string uname1 = uname;
uname = uname.Trim();
SqlCommand cmd = new SqlCommand("insert into qry_details values('" + txt_query_name.Text + "','pending for approval','" + txt_query_description.Text + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + qry + "','" + uname1 + "')", con);
cmd.ExecuteScalar();
}
check the length of qry_details table and see if its smaller than the string you send to the db?
basically the exception says you are trying to something bigger than the column length.
这篇关于数据库插入错误:“字符串或二进制数据将被截断";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:数据库插入错误:“字符串或二进制数据将被截断";
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01