VBS数据库怎么保存

VBS数据库怎么保存,第1张

参数说明:(1)strDBType(数据库类型:如ORACEL;DB2;SQL;ACCESS)

' (2)strDBAlias(数据库别名)

' (3)strUID(用户名)

' (4)strPWD(密码)

' (5)strIP(数据库IP地址:仅SQL SERVER 使用)

' (6)strLocalHostName(本地主机名:仅SQL SERVER 使用)

' (7)strDataSource(数据源:仅ACCESS使用;如d:\yysc.mdb)

通常对用户上传的图片需要保存到数据库中。解决方法一般有两种:一种是将图片保存的路径存储到数据库;另一种是将图片以二进制数据流的形式直接写入数据库字段中。以下为具体方法:

一、保存图片的上传路径到数据库:

string uppath=""//用于保存图片上传路径

//获取上传图片的文件名

string fileFullname = this.FileUpload1.FileName

//获取图片上传的时间,以时间作为图片的名字可以防止图片重名

string dataName = DateTime.Now.ToString("yyyyMMddhhmmss")

//获取图片的文件名(不含扩展名)

string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1)

//获取图片扩展名

string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1)

//判断是否为要求的格式

if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")

{

//将图片上传到指定路径的文件夹

this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type)

//将路径保存到变量,将该变量的值保存到数据库相应字段即可

uppath = "~/upload/" + dataName + "." + type

}

二、将图片以二进制数据流直接保存到数据库:

引用如下命名空间:

using System.Drawing

using System.IO

using System.Data.SqlClient

设计数据库时,表中相应的字段类型为iamge

保存:

//图片路径

string strPath = this.FileUpload1.PostedFile.FileName.ToString ()

//读取图片

FileStream fs = new System.IO.FileStream(strPath, FileMode.Open, FileAccess.Read)

BinaryReader br = new BinaryReader(fs)

byte[] photo = br.ReadBytes((int)fs.Length)

br.Close()

fs.Close()

//存入

SqlConnection myConn = new SqlConnection("Data Source=.Initial Catalog=stumanageUser ID=saPassword=123")

string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )"// *** 作数据库语句根据需要修改

SqlCommand myComm = new SqlCommand(strComm, myConn)

myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length)

myComm.Parameters["@photoBinary"].Value = photo

myConn.Open()

if (myComm.ExecuteNonQuery() >0)

{

this.Label1.Text = "ok"

}

myConn.Close()

读取:

...连接数据库字符串省略

mycon.Open()

SqlCommand command = new

SqlCommand("select stuimage from stuInfo where stuid=107", mycon)//查询语句根据需要修改

byte[] image = (byte[])command.ExecuteScalar ()

//指定从数据库读取出来的图片的保存路径及名字

string strPath = "~/Upload/zhangsan.JPG"

string strPhotoPath = Server.MapPath(strPath)

//按上面的路径与名字保存图片文件

BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate))

bw.Write(image)

bw.Close()

//显示图片

this.Image1.ImageUrl = strPath

采用俩种方式可以根据实际需求灵活选择。

如下两种方法可供选择:bat批量处理和VBA。

1、BAT批处理文件

A. 获取文件名

选定部分或全选所有文件 >Shift+右键 >复制为路径 >粘贴到Excel

B. 编辑命令符

l 替换文件名前的路径(全部替换)

l 生成如下字符(空格不可缺少): REN_原文件名_新文件名

可以使用EXCEL公式批量处理:

例如如下公式可将文件名前增加序列号1,2,3等。

=CONCATENATE("REN","",A1," ",ROW(A1),A1)生成批处理文件

C. 批处理

l 文件夹内新建txt文件

l 复制所有excel中编辑好的字符到txt文件中

l 文件扩展名更改为bat,运行。

2、VBA法

alt+F11,复制如下代码到代码栏,F5运行

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Sub Rename_()

Dim my_Path, my_Doc As String

With Application.FileDialog(msoFileDialogFolderPicker) '定位文件夹

.Show

.AllowMultiSelect = False

my_Path = .SelectedItems(1)

End With

Dim i As Single

i = 1

my_Doc = Dir(my_Path &"\" &"*") '遍历所有文件

Do While Len(my_Doc) <>0

Name my_Path &"\" &my_Doc As my_Path &"\" &i &my_Doc '更名:增加序号

i = i + 1

my_Doc = Dir

Loop

my_Doc = Dir(my_Path &"\" &"*")

i = 1

Do While Len(my_Doc) <>0 '复制到excel

Cells(i, 1) = my_Doc

i = i + 1

my_Doc = Dir

Loop


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10037216.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存