首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

问个数据存储的有关问题,就10分,别嫌少

2012-03-23 
问个数据存储的问题,就10分,别嫌少啊privatevoidbutton1_Click(objectsender,EventArgse){stringpathName

问个数据存储的问题,就10分,别嫌少啊
private   void   button1_Click(object   sender,   EventArgs   e)
                {
                        string   pathName;
                        if   (this.openFileDialog1.ShowDialog()   ==   System.Windows.Forms.DialogResult.OK)
                        {
                                pathName   =   this.openFileDialog1.FileName;
                                System.Drawing.Image   img   =   System.Drawing.Image.FromFile(pathName);
                                this.pictureBox1.Image   =   img;

                                //将图像读入到字节数组
                                System.IO.FileStream   fs   =   new   System.IO.FileStream(pathName,   System.IO.FileMode.Open,   System.IO.FileAccess.Read);
                                byte[]   buffByte   =   new   byte[fs.Length];
                                fs.Read(buffByte,   0,   (int)fs.Length);
                                fs.Close();
                                fs   =   null;
                                SqlConnection   sqlConnection1   =   new   SqlConnection( "Data   Source=.;Initial   Catalog=111;Integrated   Security=True ");
                                SqlCommand   sqlCommand1   =   new   SqlCommand();
                                //建立Command命令
                                string   comm   =   @ "Insert   into   table1(img,name)   values(@img,@name) ";
                             
                        sqlCommand1.CommandType   =   System.Data.CommandType.Text;
                              sqlCommand1.CommandText   =   comm;
                          sqlCommand1.Connection   =   sqlConnection1;
                                //创建Parameter
                            sqlCommand1.Parameters.Add( "@img ",   System.Data.SqlDbType.Image);


                              sqlCommand1.Parameters[0].Value   =   buffByte;
                            sqlCommand1.Parameters.Add( "@name ",   System.Data.SqlDbType.VarChar);
                            sqlCommand1.Parameters[1].Value   =   pathName.Substring(pathName.LastIndexOf( "\\ ")   +   1);
                                try
                                {
                                      sqlConnection1.Open();
                                        sqlCommand1.ExecuteNonQuery();
                                      sqlConnection1.Close();
                                }
                                catch   (System.Exception   ee)
                                {
                                        MessageBox.Show(ee.Message);
                                }
                                buffByte   =   null;  
                        }
                }
提示错误为:将截断字符串或二进制数据,语句已终止。麻烦谁帮忙看下,谢谢。

[解决办法]
检查一下表中的字段长度

热点排行