在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C#操作Access数据库源码

C#操作Access数据库源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.13M
  • 下载次数:298
  • 浏览次数:2430
  • 发布时间:2015-01-30
  • 实例类别:C#语言基础
  • 发 布 人:caoluyang
  • 文件格式:.zip
  • 所需积分:3

实例介绍

【实例简介】

        基于SQL语句新建,添加,查找,等操作
【实例截图】

    
【核心代码】

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using ADOX;
using System.Collections;
using System.IO;
 
namespace Access数据库
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region //access调用方法
 
        //创建mdb
        public  bool CreateMDBDataBase(string mdbPath)
        {
            try
            {
                ADOX.CatalogClass cat = new ADOX.CatalogClass();
                cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";");
                cat = null;
                return true;
            }
            catch { return false; }
        }
        // 新建mdb的表
        //mdbHead是一个ArrayList,存储的是table表中的具体列名。
        public static bool CreateMDBTable(string mdbPath, string tableName, ArrayList mdbHead)
        {
            try
            {
                ADOX.CatalogClass cat = new ADOX.CatalogClass();
                string sAccessConnection
                    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath;
                ADODB.Connection cn = new ADODB.Connection();
                cn.Open(sAccessConnection, null, null, -1);
                cat.ActiveConnection = cn;
 
                //新建一个表
                ADOX.TableClass tbl = new ADOX.TableClass();
                tbl.ParentCatalog = cat;
                tbl.Name = tableName;
 
                int size = mdbHead.Count;
                for (int i = 0; i < size; i  )
                {
                    //增加一个文本字段
                    ADOX.ColumnClass col2 = new ADOX.ColumnClass();
                    col2.ParentCatalog = cat;
                    col2.Name = mdbHead[i].ToString();//列的名称
                    col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
                    tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarWChar, 500);
                }
                cat.Tables.Append(tbl);   //这句把表加入数据库(非常重要)
                tbl = null;
                cat = null;
                cn.Close();
                return true;
            }
            catch { return false; }
        }
        //读取mdb文件内数据表个数
        public string[] GetShemaTableName(string mdbPath)
        {    
            try 
            {    
                //获取数据表
                 
                string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath;
                OleDbConnection conn = new OleDbConnection(ConnectionString);
                conn.Open();
                DataTable shemaTable = conn.GetOleDbSchemaTable
                    (OleDbSchemaGuid.Tables, new object[]{ null, null, null, "TABLE" }); 
                int n = shemaTable.Rows.Count;
                string[] strTable = new string[n];
                int m = shemaTable.Columns.IndexOf("TABLE_NAME");
                for (int i = 0; i < n; i  )
                {                 
                    DataRow m_DataRow = shemaTable.Rows[i]; 
                    strTable[i] = m_DataRow.ItemArray.GetValue(m).ToString();
                }               
                return strTable; 
            }         
            catch (OleDbException ex) 
            {            
                MessageBox.Show("指定的限制集无效:\n"   ex.Message);  
                return null;   
            }  
        }
        //读取表内列个数
        public string[] GetShemaColName(string mdbPath,string tableName)
        {
            try
            {
                //获取数据表
 
                string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath;
                OleDbConnection conn = new OleDbConnection(ConnectionString);
                conn.Open();
                
                DataTable shemaTable = conn.GetOleDbSchemaTable
                    (OleDbSchemaGuid.Columns, new object[] { null, null, tableName, null });
                //foreach (DataRow dr in shemaTable.Rows)
                //{
                //  string s=dr["TABLE_NAME"].ToString();
                //}
                //获取表内列数
                int n = shemaTable.Rows.Count;
                string[] strTable = new string[n];
                int m = shemaTable.Columns.IndexOf("COLUMN_NAME");
                for (int i = 0; i < n; i  )
                {
                    DataRow m_DataRow = shemaTable.Rows[i];
                    strTable[i] = m_DataRow.ItemArray.GetValue(m).ToString();
                }
                  
                return strTable;
            }
            catch (OleDbException ex)
            {
                MessageBox.Show("指定的限制集无效:\n"   ex.Message);
                return null;
            }
        }
        // 读取mdb数据
        public static DataTable ReadAllData(string tableName, string mdbPath, ref bool success)
        {
            DataTable dt = new DataTable();
            try
            {
                DataRow dr;
                //1、建立连接
                string strConn
                    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";Jet OLEDB:Database Password=haoren";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
                //建立SQL查询
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句
                odCommand.CommandText = "select * from "   tableName;
                //建立读取
                OleDbDataReader odrReader = odCommand.ExecuteReader();
                //查询并显示数据
                int size = odrReader.FieldCount;
                for (int i = 0; i < size; i  )
                {
                    DataColumn dc;
                    dc = new DataColumn(odrReader.GetName(i));
                    dt.Columns.Add(dc);
                }
                while (odrReader.Read())
                {
                    dr = dt.NewRow();
                    for (int i = 0; i < size; i  )
                    {
                        dr[odrReader.GetName(i)] = odrReader[odrReader.GetName(i)].ToString();
                    }
                    dt.Rows.Add(dr);
                }
                //关闭连接
                odrReader.Close();
                odcConnection.Close();
                success = true;
                return dt;
            }
            catch
            {
                success = false;
                return dt;
            }
        }
        // 读取mdb数据 (按列)
        public static DataTable ReadDataByColumns(string mdbPath, string tableName, string[] columns, ref bool success)
        {
            DataTable dt = new DataTable();
            try
            {
                DataRow dr;
                //1、建立连接
                string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";Jet OLEDB:Database Password=haoren";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
                //建立SQL查询
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句
                string strColumn = "";
                for (int i = 0; i < columns.Length; i  )
                {
                    strColumn  = columns[i].ToString()   ",";
                }
                strColumn = strColumn.TrimEnd(',');
                odCommand.CommandText = "select "   strColumn   " from "   tableName;
                //建立读取
                OleDbDataReader odrReader = odCommand.ExecuteReader();
                //查询并显示数据
                int size = odrReader.FieldCount;
                for (int i = 0; i < size; i  )
                {
                    DataColumn dc;
                    dc = new DataColumn(odrReader.GetName(i));
                    dt.Columns.Add(dc);
                }
 
                while (odrReader.Read())
                {
                    dr = dt.NewRow();
                    for (int i = 0; i < size; i  )
                    {
                        dr[odrReader.GetName(i)] = odrReader[odrReader.GetName(i)].ToString();
                    }
                    dt.Rows.Add(dr);
                }
                //关闭连接
                odrReader.Close();
                odcConnection.Close();
                success = true;
                return dt;
            }
            catch
            {
                success = false;
                return dt;
            }
        }
        //普通的节点
        public struct Node
        {
            private string nodeType;
            public string NodeType//表的字段名
            {
                set { nodeType = value; }
                get { return nodeType; }
            }
 
            private string nodeValue;
            public string NodeValue//具体的值
            {
                set { nodeValue = value; }
                get { return nodeValue; }
            }
        }
 
        //照片节点
        public struct PictureNode
        {
            private string nodeType;
            public string NodeType//照片的列名
            {
                set { nodeType = value; }
                get { return nodeType; }
            }
 
            private byte[] nodeValue;
            public byte[] NodeValue//照片的值,注意类型
            {
                set { nodeValue = value; }
                get { return nodeValue; }
            }
        }
        /* //另外的写法有错误
           
        //插入数据
        public static bool InsertRow(string mdbPath, string tableName, ArrayList insertArray,
             PictureNode pictureNode, ref string errinfo)
        {
            try
            {
                //1、建立连接
                string strConn
                    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";Jet OLEDB:Database Password=haoren";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
 
                string str_col = "";
                int size_col = insertArray.Count;
                for (int i = 0; i < size_col; i  )
                {
                    Node vipNode = new Node();
                    vipNode = (Node)insertArray[i];
                    str_col  = vipNode.NodeType   ",";
                }
                str_col = str_col.TrimEnd(',');
 
 
                int size_row = insertArray.Count;
                string str_row = "";
                for (int i = 0; i < size_row; i  )
                {
                    Node vipNode = new Node();
                    vipNode = (Node)insertArray[i];
                    string v = vipNode.NodeValue.ToString();
                    v = DealString(v);
                    if (v == "")
                    {
                        str_row  = "null"   ',';
                    }
                    else
                    {
                        str_row  = "'"   v   "'"   ',';
                    }
                }
                str_row = str_row.TrimEnd(',');
                if (pictureNode != null && pictureNode.NodeValue != null)
                {
                    str_col  = ','   pictureNode.NodeType;
                    str_row  = ",@Image";
                }
                string sql = "insert into "   tableName   @" ("   str_col   ") values"   @"("   str_row   ")";
                OleDbCommand odCommand = new OleDbCommand(sql, odcConnection);
                if (pictureNode != null && pictureNode.NodeValue != null)
                {
                    odCommand.Parameters.Add("@Image", OleDbType.VarBinary, pictureNode.NodeValue.Length).Value = pictureNode.NodeValue;
                }
                odCommand.ExecuteNonQuery();
                odcConnection.Close();
                return true;
            }
            catch (Exception err)
            {
                errinfo = err.Message;
                return false;
            }
        }
        //更新一行数据
        public static bool UpdateRow(string mdbPath, string tableName,
            Node keyNode, ArrayList insertArray, PictureNode pictureNode, ref string errinfo)
        {
            try
            {
                //1、建立连接
                string strConn
                    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";Jet OLEDB:Database Password=haoren";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
 
                int size = insertArray.Count;
                string str = "";
                for (int i = 0; i < size; i  )
                {
                    Node node = new Node();
                    node = (Node)insertArray[i];
                    string v = node.NodeValue.ToString();
                    v = DealString(v);
                    str  = node.NodeType   " = ";
                    if (v == "")
                    {
                        str  = "null"   ',';
                    }
                    else
                    {
                        str  = "'"   v   "'"   ',';
                    }
 
                }
                str = str.TrimEnd(',');
                if (pictureNode.NodeValue != null)
                {
                    str  = ','   pictureNode.NodeType;
                    str  = " = @Image";
                }
                string sql = "update "   tableName   " set "   str 
                    " where "   keyNode.NodeType   " = "   "'"   keyNode.NodeValue   "'";
                OleDbCommand odCommand = new OleDbCommand(sql, odcConnection);
                if (pictureNode.NodeValue != null)
                {
                    odCommand.Parameters.Add("@Image", OleDbType.VarBinary, pictureNode.NodeValue.Length).Value = pictureNode.NodeValue;
                }
                odCommand.ExecuteNonQuery();
                odcConnection.Close();
                return true;
            }
            catch (Exception err)
            {
                errinfo = err.Message;
                return false;
            }
        }
       */
        //插入图片数据
        public static bool InsertPictureToMDB(string mdbPath, string tableName, Node keyNode,PictureNode pictureNode, ref string errinfo)
        {
            try
            {
                //1、建立连接
                string strConn
                    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";Jet OLEDB:Database Password=haoren";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
                string sql = @"update "   tableName   " set "   pictureNode.NodeType   "="
                      "@Image where "   keyNode.NodeType   "="   "'"   keyNode.NodeValue   "'";
 
                OleDbCommand comm = new OleDbCommand(sql, odcConnection);
                byte[] pic = pictureNode.NodeValue;
                comm.Parameters.Add("@Image", OleDbType.VarBinary, pic.Length).Value = pic;
                comm.ExecuteNonQuery();
                odcConnection.Close();
                return true;
            }
            catch (Exception err)
            {
                errinfo = err.Message;
                return false;
            }
        }
        //从mdb中获得照片
        public static byte[] GetImageFromMDB(string tableName,string pictureCol, Node keyNode)
        {
            try
            {
                string sql = "Select "   pictureCol   " From "   tableName 
                    " member Where "   keyNode.NodeType   " = "
                      "'"   keyNode.NodeValue   "'";
                string strConn
                    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";Jet OLEDB:Database Password=haoren";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
                OleDbCommand comm = new OleDbCommand(sql, odcConnection);
                OleDbDataReader sdr = comm.ExecuteReader();
                sdr.Read();
 
                byte[] pic = (byte[])sdr[0];
                sdr.Close();
                odcConnection.Close();
                return pic;
            }
            catch
            {
                return null;
            }
        }
        //修改mdb的一条数据
        public static bool UpdateMDBNode(string mdbPath, string tableName, Node keyNode, Node saveNode, ref string errinfo)
        {
            try
            {
                //1、建立连接
                string strConn
                    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";Jet OLEDB:Database Password=haoren";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
 
                string sql = @"update "   tableName   " set "   saveNode.NodeType   " = '"   saveNode.NodeValue 
                    "' where "   keyNode.NodeType   " = "   "'"   keyNode.NodeValue   "'";
                OleDbCommand comm = new OleDbCommand(sql, odcConnection);
                comm.ExecuteNonQuery();
                odcConnection.Close();
                return true;
            }
            catch (Exception err)
            {
                errinfo = err.Message;
                return false;
            }
        }
         //插入数据
        public static bool InsertRow(string mdbPath, string tableName, string[] colArray, string[] dataArray, ref string errinfo)
        {
            try
            {
                //1、建立连接
                string strConn
                    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";Jet OLEDB:Database Password=haoren";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
                string col="";
                string data="";
                for (int i = 0; i < colArray.Length; i  )
                {
                    if (i==colArray.Length-1)
                    {
                        col = col   colArray[i];
                    }
                    else
                    {
                        col = col   colArray[i]   ",";
                    }
                }
                for (int i = 0; i < colArray.Length; i  )
                {
                    if (i == colArray.Length - 1)
                    {
                        data = data   "'"   dataArray[i]   "'";
                    }
                    else
                    {
                        data = data   "'"   dataArray[i]   "'"   ",";
                    }
                }
                string sql = "insert into "   tableName   " ("   col   ") values ("   data ")";
                OleDbCommand comm = new OleDbCommand(sql, odcConnection);
                comm.ExecuteNonQuery();
                odcConnection.Close();
 
                return true;
            }
            catch
            {
                return false;
            }
        }
        //删除数据
        public static bool DeleteData(string mdbPath, string tableName, Node keyNode, ref string errinfo)
        {
            try
            {
                //1、建立连接
                string strConn
                    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   mdbPath   ";Jet OLEDB:Database Password=haoren";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
                string  col=keyNode.NodeType;
                string  value=keyNode.NodeValue;
                string sql = "delete from "   tableName   " where "   col   "='"   value   "'";
                //string sql = "delete from "   tableName;
                OleDbCommand comm = new OleDbCommand(sql, odcConnection);
                comm.ExecuteNonQuery();
                odcConnection.Close();
 
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
       # region
        static string mdbPath ="";
        static string tableName = "";
        static string[] strCol=new  string[4];
        private void button2_Click(object sender, EventArgs e)
        {
            //选择创建路径
            saveFileDialog1.InitialDirectory = Environment.CurrentDirectory   "\\..\\..\\..\\";
            saveFileDialog1.ShowDialog();
            textBox1.Text = saveFileDialog1.FileName;
            mdbPath = textBox1.Text;
            CreateMDBDataBase(mdbPath);
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            tableName = textBox3.Text;
            int col =Convert .ToInt32( numericUpDown1.Value);
            ArrayList a = new ArrayList(col);
            for (int i = 0; i < col; i  )
            {
                a.Add(strCol[i]);
            }
            CreateMDBTable(mdbPath, tableName, a);
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            tableName = comboBox2.Text;
            bool bo1=false;
            DataTable dataTable=new DataTable();
            dataTable = ReadAllData(tableName, mdbPath, ref bo1);
            dataGridView1.DataSource = dataTable;
        }
 
        private void button5_Click(object sender, EventArgs e)
        {
            tableName = comboBox2.Text;
            DataTable dataTable=new DataTable();
            bool bo1=false;
            string[] columns={comboBox1.Text};
            dataTable= ReadDataByColumns( mdbPath,  tableName, columns, ref bo1);
            dataGridView1.DataSource = dataTable;
        }
 
        private void button6_Click(object sender, EventArgs e)
        {
            string bol = "";
            Node a=new Node();
            Node b = new Node();
            a.NodeType=textBox8.Text;
            a.NodeValue=textBox9.Text;
            b.NodeType = textBox8.Text;
            b.NodeValue=textBox10.Text;
            UpdateMDBNode(mdbPath, tableName, a, b, ref bol);
        }
 
        private void button7_Click(object sender, EventArgs e)
        {
            string bol = "";
            string[] colArray = GetShemaColName(mdbPath, tableName);
            string[] dataArray = { textBox5.Text, textBox6.Text, textBox7.Text };
            InsertRow(mdbPath, tableName, colArray,dataArray, ref bol);
        }
 
        private void button9_Click(object sender, EventArgs e)
        {
            string bol = "";
            Node a = new Node();
            a.NodeType = textBox11.Text;
            a.NodeValue = textBox12.Text;
           DeleteData (mdbPath, tableName, a, ref bol);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.CurrentDirectory   "\\..\\..\\..\\";
            openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;
            mdbPath = textBox1.Text;
             
            string[] strA = GetShemaTableName(mdbPath);
            textBox2.Text = strA.Length.ToString();
            for (int i = 0; i < strA.Length; i  )
            {
                comboBox2.Items.Add(strA[i ]);
            }
            
        }
 
        private void textBox4_Leave(object sender, EventArgs e)
        {
            int col = Convert.ToInt32(numericUpDown2.Value);
            strCol[col] = textBox4.Text;
        }
 
        private void comboBox1_Enter(object sender, EventArgs e)
        {
            if (comboBox1.Text=="")
            {
                //MessageBox.Show("");
                string[] strA = GetShemaColName(mdbPath, tableName);
                for (int i = 0; i < strA.Length; i  )
                {
                    comboBox1.Items.Add(strA[i]);
                }
            }
             
        }
 
        private void comboBox2_Leave(object sender, EventArgs e)
        {
            tableName = comboBox2.Text;
        }
       #endregion
        private void button8_Click(object sender, EventArgs e)
        {
            Node keyNode = new Node();
            PictureNode pictureNode = new PictureNode();
            string errinfo = "";
            FileStream myStream = new FileStream(openPicture.FileName, FileMode.Open, FileAccess.Read);
            byte[] imagebytes = new byte[myStream.Length];//fs.Length文件流的长度,用字节表示
            BinaryReader br = new BinaryReader(myStream);//二进制文件读取器
            imagebytes = br.ReadBytes(Convert.ToInt32(myStream.Length));//从当前流中将count个字节读入字节数组中
            myStream.Close();          
            keyNode.NodeType = textBox15.Text;
            keyNode.NodeValue = textBox16.Text;
            pictureNode.NodeType = textBox13.Text;
            pictureNode.NodeValue = imagebytes;
            InsertPictureToMDB(mdbPath, tableName, keyNode, pictureNode,ref errinfo);
        }
        OpenFileDialog openPicture = new OpenFileDialog();
        private void textBox14_Enter(object sender, EventArgs e)
        {
            openPicture.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.* ";
            openPicture.ShowDialog();
            textBox14.Text = openPicture.FileName;
            //openPicture.OpenFile();
        }
 
        private void button10_Click(object sender, EventArgs e)
        {
            Node keyNode = new Node();
            keyNode.NodeType = textBox15.Text;
            keyNode.NodeValue = textBox16.Text;
            string pictureCol = textBox13.Text;
            byte[] pictureByt= GetImageFromMDB(tableName, pictureCol, keyNode);
            MemoryStream stream = new MemoryStream(pictureByt, true);
            stream.Write(pictureByt, 0, pictureByt.Length);
            pictureBox1.Image = new Bitmap(stream); stream.Close();
        }
 
    }
     
}

实例下载地址

C#操作Access数据库源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 liuy5134 发表于: 2017-12-22 10:59 29
还行

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警