C# 如何将 图片直接存入SQL数据库中

C# 如何将 图片直接存入SQL数据库中,第1张

        //把文件转成二进制流出入数据库

        private void button2_Click(object sender, EventArgs e)

        {

            FileStream fs = new FileStream(textBox1Text, FileModeOpen);

            BinaryReader br = new BinaryReader(fs);

            Byte[] byData = brReadBytes((int)fsLength);

            fsClose();

            string conn = "server=;database=testDB;Uid=sa;Pwd=sa ";

            SqlConnection myconn = new SqlConnection(conn);

            myconnOpen();

            string str = "insert into pro_table (pro_name,pro_file) values('测试文件',@file)";

            SqlCommand mycomm = new SqlCommand(str, myconn);

            mycommParametersAdd("@file", SqlDbTypeBinary, byDataLength);

            mycommParameters["@file"]Value = byData;

            mycommExecuteNonQuery();

            myconnClose();

        }

        //从数据库中把二进制流读出写入还原成文件

        private void button4_Click(object sender, EventArgs e)

        {

            string conn = "server=;database=testDB;Uid=sa;Pwd=sa ";

            string str = "select pro_file from pro_table where pro_name='测试文件' ";

            SqlConnection myconn = new SqlConnection(conn);

            SqlDataAdapter sda = new SqlDataAdapter(str, conn);

            DataSet myds = new DataSet();

            myconnOpen();

            sdaFill(myds);

            myconnClose();

            Byte[] Files = (Byte[])mydsTables[0]Rows[0]["pro_file"]; 

            BinaryWriter bw = new BinaryWriter(FileOpen("D:\\2rdlc",FileModeOpenOrCreate));

            bwWrite(Files);

            bwClose();

              

        }

这样不行把。

public

static

DataSet

GetSQL(DataSet

pid)

{

string

sql

=

"select

pusername,ppurpose,pphone,pemail

from

[person]

where

";

froeach(dataRow

dr

in

pinTable[0]Row)

{

sql+="sql="+dr["pid<字段名称>"]ToString();

}

}

这样应该就可以了。

希望对你有帮助。

*** 作sql数据库的。如果是access,换成 oledb

string sConnectionString= "数据库连接";

SqlConnection objConn= new SqlConnection(sConnectionString);

objConnOpen();

SqlDataAdapter da= new SqlDataAdapter("sql语句", objConn);

DataSet ds = new DataSet();

daFill(ds);

objConnClose();

DataAdapterUpdate(DataSet) 方法

public DataSet CreateCmdsAndUpdate(string connectionString,

string queryString)

{

using (OleDbConnection connection = new OleDbConnection(connectionString))

{

OleDbDataAdapter adapter = new OleDbDataAdapter();

adapterSelectCommand = new OleDbCommand(queryString, connection);

OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

connectionOpen();

DataSet customers = new DataSet();

adapterFill(customers);

//在这里编辑DataSet后,调用下面的Update方法

adapterUpdate(customers);

return customers;

}

}

你可以用sqlcommandbuilder scb=new sqlcommandbuilder(thismycmd);

thismycmdupdate(dd);

不过你如果是直接在datagridview中更新的话你这段代码还是会有问题

1 C#中怎么把数据保存到ACCESS数据库

Sqlmand是 *** 作sql数据库的,Access用OleDbmand首先定义一个链接对象OleDbConnection conn = new OleDbConnection("[数据库连接字符串]");connOpen(); 打开数据库连接OleDbmand cmd = new OleDbmand("[Insert/Update/Delte语句]", conn);cmdExecuteNonQuery(); 执行 *** 作,如果是查询则不是用这个方法 最后别忘记关闭数据库连接和释放对象。

2 C# 如何将 直接存入SQL数据库中

把文件转成二进制流出入数据库

private void button2_Click(object sender, EventArgs e)

{

FileStream fs = new FileStream(textBox1Text, FileModeOpen);

BinaryReader br = new BinaryReader(fs);

Byte[] byData = brReadBytes((int)fsLength);

fsClose();

string conn = "server=;database=testDB;Uid=sa;Pwd=sa ";

SqlConnection myconn = new SqlConnection(conn);

myconnOpen();

string str = "insert into pro_table (pro_name,pro_file) values('测试文件',@file)";

Sqlmand mym = new Sqlmand(str, myconn);

mymParametersAdd("@file", SqlDbTypeBinary, byDataLength);

mymParameters["@file"]Value = byData;

mymExecuteNonQuery();

myconnClose();

}

从数据库中把二进制流读出写入还原成文件

private void button4_Click(object sender, EventArgs e)

{

string conn = "server=;database=testDB;Uid=sa;Pwd=sa ";

string str = "select pro_file from pro_table where pro_name='测试文件' ";

SqlConnection myconn = new SqlConnection(conn);

SqlDataAdapter sda = new SqlDataAdapter(str, conn);

DataSet myds = new DataSet();

myconnOpen();

sdaFill(myds);

myconnClose();

Byte[] Files = (Byte[])mydsTables[0]Rows[0]["pro_file"];

BinaryWriter bw = new BinaryWriter(FileOpen("D:\\2rdlc",FileModeOpenOrCreate));

bwWrite(Files);

bwClose();

}

3 c#如何把存取到SQL数据库

一楼开玩笑了!!可以保存到数据库的。

首先,你的数据库里要有一个存放二进制数据的字段。然后,用一个文件选择控件,让用户选择。

用FileStreamRead把文件按照二进制读取到byte[]中,接下来,链接数据库,用sql语句,进行相应的插入 *** 作,将数据库的二进制数据的字段赋值为byte[]就行了。以下是保存和显示的代码:private void SaveImage(string fileName) { Read the file into a byte array using (FileStream fs = new FileStream(fileName, FileModeOpen, FileAccessRead)) { byte[] imageData = new Byte[fsLength]; fsRead(imageData, 0, (int)fsLength); using (SqlConnection conn = new SqlConnection(connectionString)) { string sql = "insert into image (imagefilename,blobdata) values (@filename,@blobdata)"; Sqlmand cmd = new Sqlmand(sql, conn); cmdParametersAdd("@filename",SqlDbTypeText); cmdParameters["@filename"]Direction = ParameterDirectionInput; cmdParametersAdd("@blobdata", SqlDbTypeImage); cmdParameters["@blobdata"]Direction = ParameterDirectionInput; Store the byte array within the image field cmdParameters["@filename"]Value = fileName; cmdParameters["@blobdata"]Value = imageData; connOpen(); if (cmdExecuteNonQuery() == 1) { MessageBoxShow("Done"); } } } } private void LoadImage(string fileName) { using (SqlConnection conn = new SqlConnection(connectionString)) { string sql = "select blobdata from Image where ImageFileName like @filename"; Sqlmand cmd = new Sqlmand(sql, conn); cmdParametersAdd("@filename", SqlDbTypeText); cmdParameters["@filename"]Value = fileName; connOpen(); object objImage = cmdExecuteScalar(); byte[] buffer = (byte[])objImage; BinaryWriter bw = new BinaryWriter(new FileStream("C:\\abcd", FileModeCreate)); bwWrite(buffer); bwClose(); MemoryStream ms = new MemoryStream(buffer); Image bgImage = ImageFromStream(ms); msClose(); thisBackgroundImage = bgImage; } } ------------------------------------------------------------------PS:用空情报我踩踩空间,谢谢。

/kxl361。

4 如何将文件的路径存入SQL数据库中去

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

string strPath = @"d:\baidu\up";

string[] fileNames = SystemIODirectoryGetFiles(strPath);

SqlConnection Cn=new SqlConnection(这里写你的连接串);

Sqlmand Cmd=new Sqlmand("Insert JpgFiles values (@JpgPath)",Cn)

CmdParametersAdd("@JpgPath",SqlDbTypeVarChar,50);

foreach (string strName in fileNames)

{

CmdParameters[0]Value=strName;

CmdExecuteNoQuery();

}

5 怎样将数据存入mysql数据库

MySQL命令行导出数据库:

1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录

如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server 41\bin

(或者直接将windows的环境变量path中添加该目录)

2,导出数据库:mysqldump -u 用户名 -p 数据库名 >; 导出的文件名

如我输入的命令行:mysqldump -u root -p news > newssql (输入后会让你输入进入MySQL的密码)

(如果导出单张表的话在数据库名后面输入表名即可)

3、会看到文件newssql自动生成到bin文件下

命令行导入数据库:

1,将要导入的sql文件移至bin文件下,这样的路径比较方便

2,同上面导出的第1步

3,进入MySQL:mysql -u 用户名 -p

如我输入的命令行:mysql -u root -p (输入同样后会让你输入MySQL的密码)

4,在MySQL-Front中新建你要建的数据库,这时是空数据库,如新建一个名为news的目标数据库

5,输入:mysql>use 目标数据库名

如我输入的命令行:mysql>use news;

6,导入文件:mysql>source 导入的文件名;

如我输入的命令行:mysql>source newssql;

6 如何把文件存入到sql server 2008

1MDF文件

在企业管理器中

右击数据库

点击所有任务

附加数据库

点三个点选择文件

选中U盘中的MDF文件确定即可

2BAK等备份文件:

新建空数据库,取名最好为原数据库名

右击新建的数据库

点所有任务

点还原数据库

点从设备

点选择设备

点添加

定位到U盘中您的备份的文件

确定

点选项

点在现有数据库上强制还原

点确定

等待

完成!

另外,站长团上有产品团购,便宜有保证

以上就是关于C# 如何将 图片直接存入SQL数据库中全部的内容,包括:C# 如何将 图片直接存入SQL数据库中、遍历dataset生成SQL语句、Sql返回一个DataSet数据集,这样写是不是偶尔会获取不到数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/sjk/9648912.html

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

发表评论

登录后才能评论

评论列表(0条)

保存