我的代码有什么问题? [英] What's wrong in my code?

查看:130
本文介绍了我的代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..



任何人都可以告诉我代码中的错误...我想将图像存储到我的数据库中,但我是不能那样做。



Hi Everyone..

Can any one tell me whats wrong in my code...I would like to store images into my database, but I'm not able to do that.

private void btnaddinfo_Click(object sender, EventArgs e)
        {
            string scn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(scn))
                {
                using (SqlCommand cmd = new SqlCommand("SP_Info", cn))
                {
                    try
                    { 
                        byte[] img = null;
                        FileStream fs = new FileStream(imageloc, FileMode.Open, FileAccess.Read);
                        BinaryReader br = new BinaryReader(fs);
                        img = br.ReadBytes((int)fs.Length);     

                        cmd.CommandType = CommandType.StoredProcedure;

                        cmd.Parameters.Add("@Hp_Number", SqlDbType.NVarChar, 50).Value = tbhpnum.Text; ;
                        cmd.Parameters.Add("@Customer_Name", SqlDbType.VarChar, 50).Value = tbcusnam.Text;
                        cmd.Parameters.Add("@Customer_Contact_Number", SqlDbType.NVarChar, 15).Value = tbcusmblno.Text;
                        cmd.Parameters.Add("@Guarantor_Name", SqlDbType.VarChar, 50).Value = tbguanam.Text;
                        cmd.Parameters.Add("@Guarantor_Contact_Number", SqlDbType.NVarChar, 15).Value = tbguamblno.Text;
                        cmd.Parameters.Add("@Hp_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbhpdat.Text);
                        cmd.Parameters.Add("@Customer_Address", SqlDbType.NVarChar, Max).Value = tbcusadd.Text;
                        cmd.Parameters.Add("@Vehicle", SqlDbType.VarChar, 50).Value = tbveh.SelectedItem.ToString();
                        cmd.Parameters.Add("@Vehicle_Model", SqlDbType.VarChar, 50).Value = tbvehmod.SelectedItem.ToString();
                        cmd.Parameters.Add("@Vehicle_Number", SqlDbType.NVarChar, 50).Value = tbvehnum.Text;
                        cmd.Parameters.Add("@Chasis_Number", SqlDbType.NVarChar, 50).Value = tbchanum.Text;
                        cmd.Parameters.Add("@Engine_Number", SqlDbType.NVarChar, 50).Value = tbengnum.Text;
                        cmd.Parameters.Add("@FC_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbfcdat.Text);
                        cmd.Parameters.Add("@Insurance_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbinsdat.Text);
                        cmd.Parameters.Add("@Insurance_Amount", SqlDbType.Int).Value = Convert.ToInt32(tbinsamt.Text);
                        cmd.Parameters.Add("@Paid_Amount", SqlDbType.Int).Value = Convert.ToInt32(tbpaiamt.Text);
                        cmd.Parameters.Add("@Vehicle_Pic", SqlDbType.Image).Value = boxvehpic;
                        cmd.Parameters.Add("@Customer_Pic", SqlDbType.Image).Value = boxcuspic;
                        cmd.Parameters.Add("@Guarantor_Pic", SqlDbType.Image).Value = boxguapic;
                        cmd.Parameters.Add("@Documents_Pic", SqlDbType.Image).Value = boxdocpic;
                        cmd.Parameters.Add("@Insurance_Pic", SqlDbType.Image).Value = boxinspic;

                        if (cn.State != ConnectionState.Open)
                            cn.Open();
                        int count = cmd.ExecuteNonQuery();
                        if (count == 1)
                        {
                            MessageBox.Show(count.ToString() + "Record(s) Saved.");
                        }
                        else
                        {
                            MessageBox.Show("Please correct the error which you have entered and then click on addinformation button");
                        }
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.ToString());

                    }
                    finally
                    {
                        if (cn.State == ConnectionState.Open)
                            cn.Close();
                    }
                }
            }
        }





谢谢&关于RAJENDRAN M



Thanks & Regards RAJENDRAN M

推荐答案

正如我昨天提出的那样,当你问这个时候......也许最好使用图像数据而不是随机值。



boxvehpic,boxcuspic,boxguapic等未在您的方法中设置,因此gawd知道您将哪些内容作为图像发布到您的数据库。

昨天,您试图存储为图像的图像数据的长度。



为什么不尝试实际的图像数据?你将它加载到方法的顶部,然后这次完全忽略它!

请开始考虑代码的作用,而不是从互联网上抓取代码并进行随机修改......正如该男子所说:在软件中,希望和祈祷不是一个可行的策略(Luc Pattyn)
As I suggested yesterday when you asked this...perhaps it would be better to use teh image data instead of a random value.

boxvehpic, boxcuspic, boxguapic, etc. aren't set in your method, so gawd knows what you are posting as images to your DB.
Yesterday, it was the length of the image data that you tried to store as an image.

Why don't you try the actual image data? You load it at the top of the method and then ignore it completely this time!
Please, start to think about what the code does instead of just grabbing code from the internet and making random modifications... As the man said: "in software, hope and pray is not a viable strategy" (Luc Pattyn)


这篇关于我的代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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