数据库插入错误:“字符串或二进制数据将被截断";

Database insert error: quot;string or binary data would be truncatedquot;(数据库插入错误:“字符串或二进制数据将被截断;)

本文介绍了数据库插入错误:“字符串或二进制数据将被截断";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我登录时,我将我的用户名存储在会话中.我的要求是我想将我的用户名存储在我的数据库中.这里我将它存储在 username1 中.输入用户名后,我可以使用 response.write() 打印它,并且打印效果很好.但是,当我将它存储在数据库中时,它会产生此错误:

<上一页>**sqlException 未被用户代码处理和 cmd.ExecuteScalar() 处的异常;字符串或二进制数据将被截断.该语句已终止.**

以下是我的 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.

这篇关于数据库插入错误:“字符串或二进制数据将被截断";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:数据库插入错误:“字符串或二进制数据将被截断";

基础教程推荐