数据库插入错误:"字符串或二进制数据将被截断" [英] Database insert error: "string or binary data would be truncated"

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

问题描述

当我登录,我存放我会话中的用户名。我的要求是,我要保存我的用户名在我的数据库。在这里,我将其存储在用户名1 。当用户名输入,我可以使用的Response.Write()打印出来,它是打印完美。然而,当我将其存储在数据库中它是生产这个错误:

** SQLEXCEPTION是未处理由用户code
和异常的cmd.ExecuteScalar();
字符串或二进制数据将被截断。
该语句已终止。**

以下是我的ado.net code:

 使用(SqlConnection的CON =
    新的SqlConnection(数据源=;数据库= testdb1;集成安全性= SSPI)){

    con.Open();
    //的SqlCommand CMD =新的SqlCommand(从FileInfo的删除其中ID =+ Convert.ToInt32(Request.Params [一]),CON);

    字符串的uname =(字符串)会议[FNAME]的ToString()++会话[L-NAME]的ToString()。 //Session["fname"].ToString()++会话[L-NAME]的ToString()。

    //的Response.Write(UNAME);
    //的uname =斯里兰卡哈日;
    的uname =的uname +;
    字符串uname1 =的uname;
    的uname = uname.Trim();
    的SqlCommand CMD =新的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.

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

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