CREATE TABLE myTable_yq(Document varbinary(max),yq varchar(20))

--SELECT @xmlFileName = 'c:\TestXml.xml'

INSERT INTO myTable(Document)
SELECT * FROM
OPENROWSET(BULK N'E:\20110330110932_61311.jpg', SINGLE_BLOB) AS XMLDATA

INSERT INTO myTable(Document)
SELECT * FROM
OPENROWSET(BULK N'D:\newviewhigh\serializable.txt', SINGLE_BLOB) AS XMLDATA

select * from myTable

select * from myTable_yq

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import com.viewhigh.base.databaseconnection.entity.DatabaseConnection;
import com.viewhigh.base.databaseconnection.service.IDatabaseConnectionService;
import com.viewhigh.common.util.DBHelper;
import com.viewhigh.base.databaseconnection.service.impl.DatabaseConnectionServiceImpl; public class InsertBlobTest { public void insert2zwdt(Connection conn) throws Exception{
//IDatabaseConnectionService databaseConnectionService = new DatabaseConnectionServiceImpl();
//DatabaseConnection targetDatabaseConnection = databaseConnectionService
// .getDatabaseConnectionById("40288a5a57d13f0b0157d140652e0003"); //Connection conn = new DBHelper(targetDatabaseConnection) //ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
// oos.writeObject(user);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); //ObjectInputStream ins = new ObjectInputStream(oos); byte[] bytes = bos.toByteArray();
InputStream bis = new ByteArrayInputStream(bytes); //.getConnection();
Statement stmt = conn.createStatement();
PreparedStatement pstmt= null;
conn.setAutoCommit(false);// 取消自动提交 //================================
/*Connection conn = getConnection();
conn.setAutoCommit(false); // 设置手动提交
PreparedStatement pstmt=null; */
File file=new File("D:\\newviewhigh\\serializable.txt");
InputStream in = new FileInputStream(file);
System.out.println("test---------------->");
String sql="insert into myTable_yq(Document,yq) "
+ "values(?,?)"; pstmt=conn.prepareStatement(sql);
pstmt.setBinaryStream(1, bis, file.length());
pstmt.setString(2, "yangqing"); pstmt.execute();
conn.commit();
pstmt.close();
} public static void main(String[] args){ String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL="jdbc:sqlserver://localhost:1438;DatabaseName=ah_datai_yd"; String userName="sa"; String userPwd="sa"; try { Class.forName(driverName); Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd); System.out.println("连接数据库成功"); InsertBlobTest ib = new InsertBlobTest(); try {
ib.insert2zwdt(dbConn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } catch(Exception e) { e.printStackTrace(); System.out.print("连接失败"); } } }

  

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement; import com.viewhigh.base.databaseconnection.entity.DatabaseConnection;
import com.viewhigh.base.databaseconnection.service.IDatabaseConnectionService;
import com.viewhigh.common.util.DBHelper;
import com.viewhigh.base.databaseconnection.service.impl.DatabaseConnectionServiceImpl; public class InsertBlob { //public void insert2zwdt(String xmlFileName, Long index) throws Exception{
public void insert2zwdt(Connection conn) throws Exception{
//IDatabaseConnectionService databaseConnectionService = new DatabaseConnectionServiceImpl();
//DatabaseConnection targetDatabaseConnection = databaseConnectionService
// .getDatabaseConnectionById("40288a5a57d13f0b0157d140652e0003"); //Connection conn = new DBHelper(targetDatabaseConnection)
//.getConnection();
Statement stmt = conn.createStatement();
PreparedStatement pstmt= null;
conn.setAutoCommit(false);// 取消自动提交 //================================
/*Connection conn = getConnection();
conn.setAutoCommit(false); // 设置手动提交
PreparedStatement pstmt=null; */
File file=new File("D:\\newviewhigh\\serializable.txt");
InputStream in = new FileInputStream(file);
System.out.println("test---------------->");
String sql="insert into myTable_yq(Document,yq) "
+ "values(?,?)"; pstmt=conn.prepareStatement(sql);
pstmt.setBinaryStream(1, in, file.length());
pstmt.setString(2, "yangqing");
/* pstmt.setInt(1, index.intValue());
pstmt.setString(2, exData.getStSource());
pstmt.setString(3, exData.getStKey());
pstmt.setString(4, exData.getStVersion());
pstmt.setString(5, exData.getStValue());
pstmt.setString(6, exData.getStOp());
pstmt.setString(7, exData.getStMemo());
pstmt.setBinaryStream(8, in, file.length());
pstmt.setString(9, exData.getStDest()); */ pstmt.execute();
conn.commit();
pstmt.close();
} /*public void insert2own(ExData exData, String xmlFileName, Long index) throws Exception{
File file=new File(xmlFileName);
InputStream in = new FileInputStream(file); DataSending dataSending = new DataSending();
dataSending.setNmSeqId(index.intValue());
dataSending.setStSource(exData.getStSource());
dataSending.setStKey(exData.getStKey());
dataSending.setStVersion(exData.getStVersion());
dataSending.setStValue(exData.getStValue());
dataSending.setStOp(exData.getStOp());
dataSending.setStMemo(exData.getStMemo());
dataSending.setSendDate(new Date());
byte[] data=new byte[]{};
data=inputStreamToByte(in);
dataSending.setBlData(data);
dataSending.setStDest(exData.getStDest());
dataSendingDao.save(dataSending); } */ /*private byte[] inputStreamToByte(InputStream in) throws IOException{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int ch;
while((ch=in.read())!=-1){
baos.write(ch);
}
byte[] data=baos.toByteArray();
baos.close();
return data;
} */ public static void main(String[] args){ /*InsertBlob ib = new InsertBlob(); try {
ib.insert2zwdt();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/ String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL="jdbc:sqlserver://localhost:1438;DatabaseName=ah_datai_yd"; String userName="sa"; String userPwd="sa"; try { Class.forName(driverName); Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd); System.out.println("连接数据库成功"); InsertBlob ib = new InsertBlob(); try {
ib.insert2zwdt(dbConn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } catch(Exception e) { e.printStackTrace(); System.out.print("连接失败"); } } }

  

sqlserver二进制存储的更多相关文章

  1. mssql sqlserver 可以存储二进制数据的字段类型详解

    转自: http://www.maomao365.com/?p=6738 摘要: 下文将从数据库的数据类型着手,剖析在sqlserver数据库中可以存储二进制数据的数据类型,如下所示: mssql s ...

  2. Sqlserver数据库存储路径的修改

    Sqlserver数据库存储路径的修改 Sqlserver数据库存储路径问题:本系统sqlserver路径默认是存储在C盘目录下的,由于数据会慢慢变大和避免重装系统数据丢失等问题,最好手动将路径设置在 ...

  3. C/C++ 图像二进制存储与读取

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50782792 在深度学习时,制作样本数 ...

  4. 二进制、十六进制理解及int类型二进制存储方式

    二进制 0000 0000 0000 0000 0000 0000 0000 0001 // 2^0 0000 0000 0000 0000 0000 0000 0000 0010 // 2^1 00 ...

  5. 深入理解Sqlserver文件存储之页和应用 (转)

    我们每天都在使用数据库,我们部门使用最多的关系数据库有Sqlserver,Oracle,有没有想过这些数据库是怎么存放到操作系统的文件中的?有时候为了能够设计出最优的表结构,写出高性能的Sqlserv ...

  6. float浮点数的二进制存储方式及转换

    int和float都是4字节32位表示形式.为什么float的范围大于int? float精度为6-7位.1.66*10^10的数字结果并不是166 0000 0000 指数越大,误差越大. 这些问题 ...

  7. xml存储图片 二进制存储图片

    一.保存图片到XML文件 /// <summary> /// 保存图片到XML文件 /// </summary> private void UploadImageToXml() ...

  8. java实现文件转换成二进制存储与取出

    一.功能描述: 将文件转成二进制数据放入数据库中,需要的时候,便可以取出安装与使用. 二.数据库: 建立一个数据库字段存放转成二进制的图片,这个字段有一个要求就是要设置成blob类型的 CREATE  ...

  9. FMDB存储模型对象(以二进制存储)用NSKeyedArchiver archivedDataWithRootObject序列号,NSKeyedUnarchiver unarchiveObjectWithData反序列化(重点坑是sql语句@"insert into t_newsWithChannel (nwesName,newsType) values (?,?)")一定要用占位符

    交友:微信号 dwjluck2013 一.封装FMDB单例 (1)JLFMDBHelp.h文件 #import <Foundation/Foundation.h> #import < ...

随机推荐

  1. postgreSQL学习(一):在Linux下安装postgreSQL

    1. 安装源: $ sudo yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgd ...

  2. springboot jar包运行中获取资源文件

    1. 今天晚上写了一个程序,基于Spring boot的一个小网站,发现使用FileUtils.class.getResource(path)来获取jar包中的资源文件并不能成功,其路径很奇怪 fil ...

  3. Zabbix监控服务器硬盘状态

    安装Iptables服务: [root@localhost /]# yum install iptables-services [root@localhost /]# vim /etc/sysconf ...

  4. Docker系列教程05 容器常用命令

    https://mp.weixin.qq.com/s?__biz=MzI4ODQ3NjE2OA==&mid=2247483890&idx=1&sn=2721f08624e6de ...

  5. poj2893 M×N puzzle

    x数码难题有解性判定: 只有必要性证明,没有充分性...... 还记得那个naive至极的八数码难题吗? 它回来了! 主要是借助逆序对这一神奇的手段: 考虑把x数码写成一排时的逆序对的奇偶性: 当你左 ...

  6. Command `bundle` unrecognized. Make sure that you have run `npm install` and that you are inside a react-native project.

    呃呃,在写下面的代码时出现的问题,解决办法是npm install或者yarn,如果yarn报错,再npm install就可以了 下面的是携程App首页的样式,有轮播,我没有实现出来 代码如下: / ...

  7. python的序列化与反序列化

    ------------------------------------------------------------------- 文件的序列化与反序列化:

  8. c#中内置委托

    一.Action泛型委托 和之前委托最大区别在于不用定义委托,直接实例化委托(申明就可以),泛型其实就是选择数据类型,进行使得程序更加安全,并且也实现了委托的重载,最多选择16个参数. namespa ...

  9. JS学习笔记Day6

    一.数组 1.数组就是个容器,里面可以存放任意类型的数 2.定义数组:1)var arr = []: 2)var arr = new Array():构造函数定义方式,如果括号中有一个整数,该正数代表 ...

  10. MVC知识点汇总

    一,MVC创建控制器,视图 1新建 ASP.NET MVC web应用程序项目,在Controllers文件夹下创建控制器,MVC 5 控制器,然后打开控制器,在所对应的控制器中会存在一个Action ...