已有56人关注
为什么会找不到图片
发表在C#图书答疑 2010-08-03
是否精华
版块置顶:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace 创建数据库图像表
{
    class Program
    {
        string imageFileLocation = @"E:\图片\难忘的初恋\";
        int maxImageSize = 10000;
        SqlConnection conn = null;
        SqlCommand cmd = null;
        static void Main(string[] args)
        {   
            int i;
            Program pg = new Program();
            pg.OpenConn();
            pg.Command();
            pg.CreateImageTable();
            pg.PrepareInsertImages();
            for (i = 1; i < 17; i++)
            { pg.ExecuteInsertImages(i); }

        }
        void OpenConn()
        {
            conn = new SqlConnection(@"server=PC-201001021356;integrated security=true;database=demo");
            conn.Open(); 
        }
        void CloseConn()
        { 
            conn.Close(); 
        }
        void Command()
        {
            cmd = new SqlCommand();
            cmd.Connection = conn;
        }
        void ExecuteCommand(String cmdText)
        { 
            cmd.CommandText = cmdText;
            cmd.ExecuteNonQuery();
         }
        void CreateImageTable()
        {
            ExecuteCommand(@"create table 图像表 (图形文件 nvarchar(20),图形数据 varbinary(max))");
        }
        void PrepareInsertImages()
        {
            cmd.CommandText = @"insert into 图像表 (图形文件,图形数据) values(@imagefile,@imagedata)";
            cmd.Parameters.Add("@imagefile", SqlDbType.NVarChar, 20);
            cmd.Parameters.Add("@imagedata", SqlDbType.Image, 1000000);
            cmd.Prepare();
        }
        void ExecuteInsertImages(int imageFileNumber)
        {
            string imageFileName = "图片" + imageFileNumber.ToString() + ".gif";
            byte[] imageData = loadimageFile(imageFileName, imageFileLocation, maxImageSize);
            cmd.Parameters["@imagefile"].Value = imageFileName;
            cmd.Parameters["@imagedata"].Value = imageData;
        }
        byte[] loadimageFile(string fileName, string fileLocation, int maxSize)
        { 
            byte[] imagebytes = null;
            string fp = fileLocation + fileName;
            FileStream fs = new FileStream(fp, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            imagebytes = br.ReadBytes(maxSize);
            return imagebytes;
        }
    }
}
其中这一句string imageFileName = "图片" + imageFileNumber.ToString() + ".gif";我一运行 它就会提示我没有找到图片路经 我确定 我的文件名字正确 路径也是对的 为什么就是找不到呢 数据库里的表也创建出来了很正常 就是找不到图片路径   求教高手啊
分享到:
精彩评论 1
满园春色关不住
学分:0 LV1
TA的每日心情
努力自律
2023-11-19 09:49:04
2010-08-03
沙发
读者朋友:
你好,对于你提出的问题,可以采用如下方法解决。
问:
这一句string imageFileName = "图片" + imageFileNumber.ToString() + ".gif";
我一运行 它就会提示我没有找到图片路经 我确定我的文件名字正确路径也是对的
为什么就是找不到呢
答:
可以在VS2008中多次按下F11键,单步调试,查看文件路径是否正确。比如可以单步
调试到:
string imageFileName = "图片" + imageFileNumber.ToString() + ".gif";
这一句,查看imageFileName变量中的字符串的路径是否正确。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照