首页 > 数据库技术 > 详细

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

时间:2014-05-21 20:41:32      阅读:447      评论:0      收藏:0      [点我收藏+]
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);
            }
        }

 

C#:向SqlServer数据库中插入imange类型,布布扣,bubuko.com

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

原文:http://www.cnblogs.com/shenchao/p/3739568.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!