我得到异常:字符串或二进制数据将被截断。该语句已终止。 [英] Am getting exception : String or binary data would be truncated. The statement has been terminated.

查看:86
本文介绍了我得到异常:字符串或二进制数据将被截断。该语句已终止。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图在数据库中插入数据。



它插入数据数据库表。



但是它正在抛出Exception SqlException是unhandeled。(字符串或二进制数据将被截断。语句已被终止。)



,每当点击插入按钮时,它会重复插入数据。



任何人都可以帮我解决这个问题。< br $>


Hi,

am trying to insert data in database.

its inserting the data in database table.

but it''s throwing Exception SqlException was unhandeled.(String or binary data would be truncated. The statement has been terminated.)

and when every am clicking insert button its inserting data repetaedly.

can anyone help me in solving this query.

ServiceController[] services = ServiceController.GetServices();
{
              dataGridView1.DataSource = services ;
}



 string conn = "Data Source=HostName;Initial Catalog=MSNETDB;Integrated Security=True;Pooling=False";
            SqlConnection mycon = new SqlConnection(conn);
            mycon.Open();
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    
                    string Displayname = row.Cells[3].Value.ToString();
                    string ServiceName = row.Cells[4].Value.ToString();
                    string Status = row.Cells[5].Value.ToString();
                    string ServiceType = row.Cells[6].Value.ToString();              
                    
    
                    SqlCommand com = new SqlCommand(conn, mycon);
                   
                    com .CommandText = "insert into SystemServiceInfo(DisplayName,ServiceName,Status,ServiceType)values('" + Displayname + "','" + ServiceName + "','" + Status + "','" + ServiceType + "')";
                    
                   com.ExecuteNonQuery();
                    com.Parameters.Clear();
                    com.Dispose();


                }
            
                mycon.Close();
                mycon.Dispose();





谢谢&问候

sam.198979



Thanks & Regards
sam.198979

推荐答案

看来你工作的四个列中的任何一列的maxlength都小于你插入的列数其实。

你检查过插入的数据了吗?你的意思是即使在这个异常之后实际插入了数据吗?
It seems the any of the four columns on which you are working have maxlength less than what you are inserting actually.
Have you checked the inserted data ? do you mean that the data is actually inserted even after this exception ?


请检查字段的数据长度

Please check the Data Length of the Fields
Quote:

数据库中的DisplayName,ServiceName,Status,ServiceType

DisplayName,ServiceName,Status,ServiceType

,并确保您传递的数据不超过给定长度。





如果需要,可以在数据表字段中进行相应的更改。

in Database and make sure the data you pass does not exceeds the given length.


If needed make changes accordingly in the Datatable Fields.


检查这个

字符串或二进制数据将被截断。该语句已被终止。请解释一下这个例外情况。 [ ^ ]


这篇关于我得到异常:字符串或二进制数据将被截断。该语句已终止。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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