C#:向SqlServer数据库中插入imange类型

?





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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

using
System;

using
System.Collections.Generic;

using
System.Linq;

using
System.Runtime.InteropServices;

using
System.Text;

namespace
Common

{

    public
class ConvertData

    {

        /// <summary>

        /// 构造函数

        /// </summary>

        public
ConvertData()

        {

        }

        /// <summary>

        /// 将byte[]数组转换为double[]数组

        /// </summary>

        /// <param name="b"></param>

        /// <returns></returns>

        public
double[]  BytesToDoubles(byte[] b)

        {

            // Initialize unmanged memory to hold the array.

            int
size = Marshal.SizeOf(b[0]) * b.Length;

            IntPtr pnt = Marshal.AllocHGlobal(size);

            try

            {

                // Copy the array to unmanaged memory.

                Marshal.Copy(b, 0, pnt, b.Length);

                // Copy the unmanaged array back to another managed array.

                double[] managedArray2  = new
double[b.Length / 8];

                Marshal.Copy(pnt, managedArray2, 0, b.Length / 8);

                return
managedArray2;

            }

            finally

            {

                // Free the unmanaged memory.

                Marshal.FreeHGlobal(pnt);

            }

        }

        /// <summary>

        /// 获取内存中double[],并转换为String字符串

        /// </summary>

        /// <param name="b"></param>

        /// <returns></returns>

        public
string DoublesToString(double[] doubleArray)

        {

            string
values = "";

           

            for
(int
i = 0; i < doubleArray.Length; i++)

            {

                values += doubleArray[i] + ",";

            }

           

            return
values;

        }

        /// <summary>

        /// 获取内存中double[],并转换为String字符串

        /// </summary>

        /// <param name="b"></param>

        /// <returns></returns>

        public
string BytesToString(byte[] b)

        {

            double[] doubleArray = BytesToDoubles(b);

           

            return
DoublesToString(doubleArray);

           

        }

        /// <summary>

        /// 获取内存中double[],并转换为String字符串

        /// </summary>

        /// <param name="b"></param>

        /// <returns></returns>

        public
string GetByteToDouble(byte[] b)

        {

            // Initialize unmanged memory to hold the array.

            int
size = Marshal.SizeOf(b[0]) * b.Length;

            IntPtr pnt = Marshal.AllocHGlobal(size);

            string
values = "";

            try

            {

                // Copy the array to unmanaged memory.

                Marshal.Copy(b, 0, pnt, b.Length);

                // Copy the unmanaged array back to another managed array.

                double[] managedArray2 = new
double[b.Length / 8];

                Marshal.Copy(pnt, managedArray2, 0, b.Length / 8);

                for
(int
i = 0; i < managedArray2.Length; i++)

                {

                    values += managedArray2[i] + ",";

                }

            }

            finally

            {

                // Free the unmanaged memory.

                Marshal.FreeHGlobal(pnt);

            }

            return
values;

        }

        /// <summary>

        /// 将double[]数组转换为byte[]数组

        /// </summary>

        /// <param name="d"></param>

        /// <returns></returns>

        public
byte[] DoublesToBytes(double[] d)

        {

            int
size = Marshal.SizeOf(d[0]) * d.Length;

            IntPtr pnt = Marshal.AllocHGlobal(size);

            try

            {

                // Copy the array to unmanaged memory.

                Marshal.Copy(d, 0, pnt, d.Length);

                // Copy the unmanaged array back to another managed array.

                byte[] managedArray2 = new
byte[d.Length * 8];

                Marshal.Copy(pnt, managedArray2, 0, d.Length * 8);

                return
managedArray2;

            }

            finally

            {

                // Free the unmanaged memory.

                Marshal.FreeHGlobal(pnt);

            }

        }

    }

}

调用:

?





1

2

3

4

5

6

7

8

9

10

11

12

13

public
int InsertWSWSUH(string
wscd, int
uintv, int
udrp, string
unitnm, int
usteps, double[] uhdata, double
mfp,/*datetime,*/int
nsteps) //// <param name="datetime">时间戳</param>

       {

           string
sqlStr = "Insert into [HyModelDB_new].[dbo].[FFM_WSWSUH_B] ([WSCD],[UINTV],[UDRP],[UNITNM] ,[USTEPS] ,[UHDATA],[MFP],[DATETM],[NSTEPS])  values (‘"
+ wscd + "‘,"
+ uintv + ","
+ udrp + ",‘"
+ unitnm + "‘,"
+ usteps + ","
+ "@unitData"
+ ","
+ mfp + ","
+ "@dateTime"
+ ","
+ nsteps + ")";

           

           SqlParameter[] parameters = {

                   new
SqlParameter("@unitData", SqlDbType.Image),

                   new
SqlParameter("@dateTime", SqlDbType.DateTime)};

           parameters[0].Value = ConvertData.DoublesToBytes(uhdata);

           parameters[1].Value = DateTime.Now.ToString("yyyy-MM-dd");

           return
db.ExecuteSql(sqlStr);

       }

参考代码:

?





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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

/// <summary>

        /// 增加一条数据

        /// </summary>

        public
int Add(OA.Model.InformationOfDoc model)

        {

            StringBuilder strSql=new
StringBuilder();

            strSql.Append("insert into InformationOfDoc(");

            strSql.Append("DocName,DocType,RwID,RWType,RWClass,Execution,DocBLOB,DocAttribute,CreateYMD,Remark)");

            strSql.Append(" values (");

            strSql.Append("@DocName,@DocType,@RwID,@RWType,@RWClass,@Execution,@DocBLOB,@DocAttribute,@CreateYMD,@Remark)");

            strSql.Append(";select @@IDENTITY");

            SqlParameter[] parameters = {

                    new
SqlParameter("@DocName", SqlDbType.VarChar,40),

                    new
SqlParameter("@DocType", SqlDbType.VarChar,100),

                    new
SqlParameter("@RwID", SqlDbType.VarChar,18),

                    new
SqlParameter("@RWType", SqlDbType.Char,10),

                    new
SqlParameter("@RWClass", SqlDbType.Char,4),

                    new
SqlParameter("@Execution", SqlDbType.Char,6),

                    new
SqlParameter("@DocBLOB", SqlDbType.Image),

                    new
SqlParameter("@DocAttribute", SqlDbType.Char,6),

                    new
SqlParameter("@CreateYMD", SqlDbType.Date,3),

                    new
SqlParameter("@Remark", SqlDbType.VarChar,200)};

            parameters[0].Value = model.DocName;

            parameters[1].Value = model.DocType;

            parameters[2].Value = model.RwID;

            parameters[3].Value = model.RWType;

            parameters[4].Value = model.RWClass;

            parameters[5].Value = model.Execution;

            parameters[6].Value = model.DocBLOB;

            parameters[7].Value = model.DocAttribute;

            parameters[8].Value = model.CreateYMD;

            parameters[9].Value = model.Remark;

            object
obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);

            if
(obj == null)

            {

                return
0;

            }

            else

            {

                return
Convert.ToInt32(obj);

            }

        }

时间: 2024-11-06 02:34:49

C#:向SqlServer数据库中插入imange类型的相关文章

向数据库中插入一个DateTime类型的数据到一个Date类型的字段中,需要转换类型。TO_DATE(&#39;{0}&#39;,&#39;YYYY-MM-DD&#39;))

需要指出的是,C#中有datetime类型,但是这个类型是包括小时,分钟,秒的.这个格式与数据库中的Date类型不符,如果将now设为datetime类型插入数据会失败. 需要通过TO_DATE('字段','YYYY-MM-DD'))转换.如下: string.Format("insert into tablename (TIME) values(TO_DATE('{0}','YYYY-MM-DD'))",now) 错误写法: string.Format("insert in

java.sql.date与java.util.date区别以及数据库中插入带时分秒的时间

java.sql.Date,java.sql.Time和java.sql.Timestamp三个都是java.util.Date的子类(包装类). java.sql.Date是java.util.Date的子类,是一个包装了毫秒值的瘦包装器,允许 JDBC 将毫秒值标识为 SQL DATE 值.毫秒值表示自 1970 年 1 月 1 日 00:00:00 GMT 以来经过的毫秒数. 为了与 SQL DATE 的定义一致,由 java.sql.Date 实例包装的毫秒值必须通过将时间.分钟.秒和毫

sqlserver 数据库中时间函数的建立

create function [dbo].[HtoSec](@lvalue as int)RETURNS intBEGINDECLARE @temp intSet @temp = @lvalue * 60 * 60RETURN @tempEND create function [dbo].[GetTime](@dtmValue as datetime)RETURNS intBEGINDECLARE @temp intDECLARE @GMT_TIMEZONE intSET @GMT_TIMEZ

Oracle数据库中的blob类型解析

Oracle的Blob字段比较特殊,他比long字段的性能要好很多,可以用来保存例如图片之类的二进制数据. 写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cursor,你必须使用cursor对blob进行操作,因而你在写入Blob之前,必须获得cursor才能进行写入,那么如何获得Blob的cursor呢? 这需要你先插入一个empty的blob,这将创建一个blob的cursor,然后你再把这个empty的blob的cursor用select查询出来,这样通过两步操作

解决getJdbcTemplate往oracle数据库中插入数据返回主键出错问题

我们使用Spring中的JdbcDaoSupport往Mysql中插入数据并返回主键代码,我们使用的mysql数据库,主键在数据库中设置为自增长:该类继承自JdbcDaoSupport,所以能直接使用getJdbcTemplate() public int saveUser(String userName,int age,String password){ getJdbcTemplate().update(new PreparedStatementCreator() { public Prepa

获取sqlserver数据库中所有库、表、字段名的方法

1.获取所有数据库名: SELECT Name from Master..SysDatabases ORDER BY Name 2.获取所有表名: SELECT Name from DatabaseName..SysObjects Where XType='U' ORDER BY Name XType='U':表示所有用户表; XType='S':表示所有系统表; 3.获取所有字段名: SELECT Name from SysColumns WHERE id=Object_Id('TableNa

向mysql中插入Date类型的数据

先看数据库表的定义 date字段为sql.date类型.我要向其中插入指定的日期和当前日期. 一.插入当前日期 思路:先获取当前系统,在将当前系统时间转换成sql类型的时间,然后插入数据库.代码如下 public static void insert_now() throws ClassNotFoundException, SQLException{ java.util.Date utilDate = new Date(); //获取java.util.Date对象---也即当前时间 java.

批量解密SQLSERVER数据库中的各种对象的工具dbForge SQL Decryptor

原文:批量解密SQLSERVER数据库中的各种对象的工具dbForge SQL Decryptor 批量解密SQLSERVER数据库中的各种对象的工具dbForge SQL Decryptor2.1.11 之前写过一篇文章,使用redgate公司的SQL PROMPT工具,但是不太方便 SQLPROMPT5.3对各种加密对象的解密测试 SQL2005解密已经被加密的存储过程 昨天ahdung 童鞋介绍了这个工具给我,非常感谢他 dbForge SQL Decryptor这个工具的软件公司是dev

向数据库中插入10000条数据

今天,在进行项目测试的时候,想要事先在数据库中插入10000条数据.之前在SQLServer的期末项目中也使用过,但是却也记不清了. DELIMITER $$ --定义结束符为"$$", DROP PROCEDURE IF EXISTS `proc_auto_insertdata`$$ CREATE PROCEDURE `proc_auto_insertdata`() BEGIN DECLARE pid INTEGER DEFAULT 1; WHILE pid <= 10000