未分配的lcal变量 [英] unassigned lcal variable

查看:73
本文介绍了未分配的lcal变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

 

namespace image
{
   public partial class Form1 : Form
    {
        public Form1()
        {
           InitializeComponent();
         }

        private void button1_Click(object sender, EventArgs e)
        {
            //use filestream object to read the image.



            //read to the full length of image to a byte array.



            //add this byte as an oracle parameter and insert it into database.
            
            //SqlDataAdapter empadap1;
            //DataSet dset;


            try
            {



                //proceed only when the image has a valid path


                string Image;
                if (Image != "")
                {



                    FileStream fs;



                    fs = new FileStream(@Image, FileMode.Open, FileAccess.Read);



                    //a byte array to read the image



                    byte[] picbyte = new byte[fs.Length];



                    fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));



                    fs.Close();



                    //open the database using odp.net and insert the data



                    //string connstr = @"Data Source=.;Initial Catalog=TestImage;

               // Persist Security Info=True;User ID=sa";



                    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Kiran\My Documents\image.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");



                    conn.Open();



                    string query;



                    query = "insert into test_table(id_image,pic) values(" +

                    textBox1.Text + "," + " @pic)";



                    SqlParameter picparameter = new SqlParameter();



                    picparameter.SqlDbType = SqlDbType.Image;



                    picparameter.ParameterName = "pic";



                    picparameter.Value = picbyte;



                    SqlCommand cmd = new SqlCommand(query, conn);



                    cmd.Parameters.Add(picparameter);



                    cmd.ExecuteNonQuery();



                    MessageBox.Show("Image Added");



                    cmd.Dispose();



                    conn.Close();



                    conn.Dispose();



                    //Connection();



                }



            }



            catch (Exception ex)
            {



                MessageBox.Show(ex.Message);



            }

 
        }
    }
}





此代码显示未分配的局部变量Image的错误请尽快提供我的解决方案,如果可能的话请给我无错误的图像存储和从sql中恢复的代码db 2005 for c#without stoared processure.pls.pls。



this code shows the error of "unassigned local variable Image" pls provide me solution as early as possible and pls if possible then give me error free code of image storing and retriving from sql db 2005 for c# without stoared processure.pls.pls.

推荐答案

这里你没有给图像赋值。

string Image;

之前检查是否(Image!=)这个条件.try给出类似

string Image = string.empty;

其他明智的给Image一个值。
here you are not giving any values to image .
string Image;
before checking if (Image != "") this condition .try to give like
string Image=string.empty;
other wise give a value to Image.


HI,



因为你没有为图像变量分配任何值,这就是它显示的原因你错了。

你能告诉我为什么你在这里添加了以下行没有用。





As you are not assigning any value to image variable thats why it is showing you error.
can you tell me why you have added the following line here which is of no use.

string Image;
if (Image != "")
{





如果您没有在代码中使用字符串变量,那么添加它的含义是什么你的代码?如果你真的不想使用它,请删除该代码。



谢谢



If you are not using the string variable in your code then whats the meaning of adding it to your code? remove that code if u really don''t want to use that.

Thanks


这篇关于未分配的lcal变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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