如何将文件的路径存入SQL数据库中去?(用C#编)

来源:百度知道 编辑:UC知道 时间:2024/06/07 15:55:47
我现在在学asp.net,在学习中遇到这样的一个问题:
例如我在c:\baidu\up下有用来存客户上传的图片,里面有很多文件,如1.jpg 2.jpg 3.jpg 4.jpg等等的文件,现在我要取出这些文件的绝对路径并存到数据库里面该怎么做呢。请写出一个例子的具体代码。

建一个表:JpgFiles,其中至少包含一个列:JpgPath,用来存放绝对路径的字符串,所以这个列需要用varchar,长度假设为50,不够的话自己再增加。

string strPath = @"d:\baidu\up";
string[] fileNames = System.IO.Directory.GetFiles(strPath);
SqlConnection Cn=new SqlConnection(这里写你的连接串);
SqlCommand Cmd=new SqlCommand("Insert JpgFiles values (@JpgPath)",Cn)
Cmd.Parameters.Add("@JpgPath",SqlDbType.VarChar,50);
foreach (string strName in fileNames)
{
Cmd.Parameters[0].Value=strName;
Cmd.ExecuteNoQuery();
}

string strPath = @"d:\baidu\up";
string[] fileNames = System.IO.Directory.GetFiles(strPath);
foreach (string strName in fileNames)
{
this.clbFiles.Items.Add(strName);
}

clbFiles is CheckedListBox

使用上传控件
string path = Server.MapPath("相对路径");
可以获取绝对路径

不明白你意思。c:\baidu\up\1.jpg把这个按string存进去不久可以了吗?