C#对SQL Server常见操作
1
类中 soSqlEdit.cs
2
//########################################################################
3
// 目的: 新建和删除常用数据库: CreateDB/DelDB
4
// 新建和删除常用数据库中的表: AddTable/DelTable
5
// 新建和删除常用数据库中表的字段: AddTableField:/DelTableField
6
// 对数据库进行备份和还原: BackUpDataBase/DbRestore
7
// 对数据库字段值进行修改: UpdaterDB
8
// 对数据库字段值进行查询: GetDataSet
9![]()
10
// 输入: strHostName: 主机名
11
// strDbName: 数据库名
12
// strSa: 服务器登录名
13
// strSaPwd: 服务器登录密码
14
// nDbType : 连接的数据库类型 (1:Miscrosoft SQL server)
15
// strSqlUpdate: 数据库操作字符串
16![]()
17
// 返回: 返回查询结果,并以数据集显示
18
//########################################################################
19![]()
20![]()
21![]()
22
using System;
23
using System.Collections.Generic;
24
using System.Text;
25
using System.Data;
26
using System.Data.SqlClient;
27
using System.Windows.Forms;
28
namespace SoSqlEdit
29
{
30
public class Sql
31
{
32
/// <summary>
33
///
34
/// </summary>
35
public static string strSub; //程序过程函数名
36
/// <summary>
37
///
38
/// </summary>
39
public static string strCls = "ControlIni"; //代码文件名
40![]()
41
public static string strErrs; //运行说明
42
public static Boolean bShowMessageBox = false; //是否弹出错误提示框(默认不弹出)
43
bool bOperation;
44![]()
45
public string strConnection; //连接字符串
46
public string strCmd; //操作字符串
47
48![]()
49
public SqlConnection sqlConn; //连接对象
50
public SqlCommand sqlCmd; //操作对象
51
DataSet ds = new DataSet(); //记录集对象
52
/// <summary>
53
/// 对数据库更新
54
/// </summary>
55
/// <param name="strHostName">主机名</param>
56
/// <param name="strDbName">数据库名</param>
57
/// <param name="strSa">数据库用户名</param>
58
/// <param name="strSaPwd">数据库登录密码</param>
59
/// <param name="strTableName">表名</param>
60
/// <param name="strUpdater">修改字段</param>
61
/// <param name="strReson">修改条件</param>
62
/// <param name="nDbType">数据库类型</param>
63
/// <returns></returns>
64
public bool UpdaterDB(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName,string strUpdater,string strReson,int nDbType)
65
{
66
switch (nDbType)
67
{
68
case 1:
69
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);
70
break;
71
case 2:
72
break;
73
default:
74
break;
75
}
76
try
77
{
78
//建立数据库操作语句
79
strCmd = "update " + strTableName + " set "+strUpdater +" where " + strReson;
80
//调用自定义函数,打开 操作 关闭数据库
81
bOperation = OperateDataBase();
82
if (bOperation == true)
83
return true; //操作成功,返回真
84
else
85
return false;
86
}
87
catch
88
{
89
return false; //操作失败,返回flase
90
}
91
finally
92
{
93![]()
94
}
95
96
}
97![]()
98
//
99
/// <summary>
100
/// 根据条件得到查询结果,返回数据集
101
/// </summary>
102
/// <param name="strHostName">主机名</param>
103
/// <param name="strDbName">数据库名</param>
104
/// <param name="strSa">数据库用户名</param>
105
/// <param name="strSaPwd">数据库登录密码</param>
106
/// <param name="strResult">查询列名</param>
107
/// <param name="strTableName">表名</param>
108
/// <param name="strReson">查询条件</param>
109
/// <param name="nDbType">数据库类型</param>
110
/// <returns></returns>
111
public DataSet GetDataSet(string strHostName, string strDbName, string strSa, string strSaPwd, string strResult, string strTableName, string strReson, int nDbType)
112
{
113
switch (nDbType)
114
{
115
case 1:
116
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);
117
break;
118
case 2:
119
break;
120
default: break;
121
}
122
SqlConnection conn = new SqlConnection(strConnection);
123
try
124
{
125
string strFind = "select " + strResult + " from " + strTableName + " where " + strReson;
126
conn.Open();// 打开数据库连接
127
SqlDataAdapter da = new SqlDataAdapter(strFind, conn);// 创建数据适配器
128
da.Fill(ds);
129
return ds;
130
}
131
catch
132
{
133
return null;
134
}
135
finally
136
{
137
conn.Close();
138
}
139![]()
140
}
141![]()
142
/// <summary>
143
/// 建立数据库
144
/// </summary>
145
/// <param name="strHostName">主机名</param>
146
/// <param name="strNewDbName">数据库名</param>
147
/// <param name="strDbPath">路径名</param>
148
/// <param name="nDbType">数据库类型</param>
149
/// <returns></returns>
150
public bool CreateDB(string strHostName, string strNewDbName, string strDbPath, int nDbType)
151
{
152
switch (nDbType)
153
{
154
case 1:
155
strConnection = GetStrConnection(strHostName,"","","",1);
156
break;
157
case 2:
158
strConnection = GetStrConnection(strHostName, "", "", "", 2);
159
break;
160
default: break;
161
}
162
try
163
{
164
strCmd = "CREATE DATABASE " + strNewDbName + " ON PRIMARY" + "(name=" + strNewDbName + ", filename ='" + strDbPath + strNewDbName + ".mdf', size=3," + "maxsize=5, filegrowth=10%)";
165
//调用自定义函数,打开 操作 关闭数据库
166
bOperation = OperateDataBase();
167
if (bOperation == true)
168
return true;
169
else
170
return false;
171
}
172
catch
173
{
174
return false; //操作失败,返回flase
175
}
176
finally
177
{
178
}
179
}
180
181
/// <summary>
182
/// 删除指定数据库
183
/// </summary>
184
/// <param name="strHostName">主机名</param>
185
/// <param name="strDbName">数据库名</param>
186
/// <param name="nDbType">数据库类型</param>
187
/// <returns></returns>
188
public bool DelDataBase(string strHostName, string strDbName, int nDbType)
189
{
190
switch (nDbType)
191
{
192
case 1:
193
strConnection = GetStrConnection(strHostName,"","","",1);
194
break;
195
case 2:
196
break;
197
default :
198
break;
199
}
200
try
201
{
202
strCmd = "drop database " + strDbName; //建立操作字符串
203
bOperation = OperateDataBase();
204
if (bOperation == true)
205
return true;
206
else
207
return false;
208
}
209
catch
210
{
211
return false; //操作失败,返回flase
212
}
213
finally
214
{
215
}
216
}
217
//
218
/// <summary>
219
/// 向数据库中添加表
220
/// </summary>
221
/// <param name="strHostName">主机名</param>
222
/// <param name="strDbName">数据库名</param>
223
/// <param name="strSa">数据库用户名</param>
224
/// <param name="strSaPwd">数据库登录密码</param>
225
/// <param name="strTableName">表名</param>
226
/// <param name="nDbType">数据库类型</param>
227
/// <returns></returns>
228
public bool AddTable(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName, int nDbType)
229
{
230
strSub = "AddTable";
231
switch (nDbType)
232
{
233
case 1:
234
strConnection = GetStrConnection(strHostName,strDbName, strSa, strSaPwd, nDbType);
235
break;
236
case 2: break;
237
default: break;
238
}
239
try
240
{
241
//建立表操作语句
242
strCmd = "CREATE TABLE "+strTableName + "(id char(50))";
243
//调用自定义函数,打开 操作 关闭数据库
244
bOperation = OperateDataBase();
245
if (bOperation == true)
246
return true;
247
else
248
return false;
249
}
250![]()
251
catch
252
{
253
strErrs = "添加数据库的表失败!";
254
SubError(strHostName, strDbName, strSa, "", strErrs);
255
return false; //操作失败,返回flase
256
}
257
finally
258
{
259
//程序运行日志
260
SubLog(strSub);
261
}
262
}
263
//
264
/// <summary>
265
/// 删除数据库中表
266
/// </summary>
267
/// <param name="strHostName">主机名</param>
268
/// <param name="strDbName">数据库名</param>
269
/// <param name="strSa">数据库用户名</param>
270
/// <param name="strSaPwd">数据库登录密码</param>
271
/// <param name="strTableName">表名</param>
272
/// <returns></returns>
273
public bool DelTable(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName, int nDbType)
274
{
275
strSub = "DelTable";
276
switch (nDbType)
277
{
278
case 1:
279
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);
280
break;
281
default :break ;
282
}
283
try
284
{
285
//删除数据库表操作语句
286
strCmd = "DROP TABLE " + strTableName;
287
//调用自定义函数,打开 操作 关闭数据库
288
bOperation = OperateDataBase();
289
if (bOperation == true)
290
return true;
291
else
292
return false;
293
}
294![]()
295
catch
296
{
297
strErrs = "删除数据库的表失败!";
298
SubError(strHostName, strDbName, strSa, "", strErrs);
299
return false; //操作失败,返回flase
300
}
301
finally
302
{
303
//程序运行日志
304
SubLog(strSub);
305
}
306
}
307
308
/// <summary>
309
/// 向数据库中添加字段值
310
/// </summary>
311
/// <param name="strHostName">主机名</param>
312
/// <param name="strDbName">数据库名</param>
313
/// <param name="strSa">数据库用户名</param>
314
/// <param name="strSaPwd">数据库登录密码</param>
315
/// <param name="strTableName">表名</param>
316
/// <param name="strFieldName">字段名</param>
317
/// <param name="strFieldType">字段类型</param>
318
/// <param name="nDbType">数据库类型</param>
319
/// <returns></returns>
320
public bool AddTableField(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName,string strFieldName,string strFieldType, int nDbType)
321
{
322
strSub = "AddTableField";
323
switch (nDbType)
324
{
325
case 1:
326
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);
327
break;
328
default: break;
329
}
330
try
331
{
332
//增加表字段操作语句
333
strCmd = "ALTER TABLE "+strTableName +" ADD "+strFieldName +" "+ strFieldType ;
334
//调用自定义函数,打开 操作 关闭数据库
335
bOperation = OperateDataBase();
336
if (bOperation == true)
337
return true;
338
else
339
return false;
340
}
341![]()
342
catch
343
{
344
strErrs = "添加数据库字段失败!";
345
SubError(strHostName, strDbName, strSa, "", strErrs);
346
return false; //操作失败,返回flase
347
}
348
finally
349
{
350
//程序运行日志
351
SubLog(strSub);
352![]()
353
}
354
}
355
//
356
/// <summary>
357
/// 删除数据库中指定字段值
358
/// </summary>
359
/// <param name="strHostName">主机名</param>
360
/// <param name="strDbName">数据库名</param>
361
/// <param name="strSa">数据库用户名</param>
362
/// <param name="strSaPwd">数据库登录密码</param>
363
/// <param name="strTableName">表名</param>
364
/// <param name="strFieldName">字段名</param>
365
/// <param name="nDbType">数据库类型</param>
366
/// <returns></returns>
367
public bool DelTableField(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName, string strFieldName, int nDbType)
368
{
369
strSub = "DelTableField";
370
switch (nDbType)
371
{
372
case 1:
373
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);
374
break;
375
case 2:
376
break;
377
default: break;
378
}
379
try
380
{
381
//删除表字段操作语句
382
strCmd = "alter table "+strTableName + " drop column "+strFieldName;
383
//调用自定义函数,打开 操作 关闭数据库
384
bOperation = OperateDataBase();
385
if (bOperation == true)
386
return true;
387
else
388
return false;
389
}
390
catch//(Exception Ex) throw Ex;
391
{
392
strErrs = "删除字段失败!";
393
SubError(strHostName, strDbName, strSa, "", strErrs);
394
return false; //操作失败,返回flase
395
}
396
finally
397
{
398
//程序运行日志
399
SubLog(strSub);
400
}
401
}
402
/// <summary>
403
///
404
/// </summary>
405
/// <param name="strHostName"></param>
406
/// <param name="strDbName"></param>
407
/// <param name="strSa"></param>
408
/// <param name="strSaPwd"></param>
409
/// <param name="strTableName"></param>
410
/// <param name="strFieldName"></param>
411
/// <param name="strFieldVaule"></param>
412
/// <param name="nDbType"></param>
413
/// <returns></returns>
414
public bool AddRecord(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName, string strFieldName,string strFieldVaule, int nDbType)
415
{
416
strSub = "AddRecord";
417
switch (nDbType)
418
{
419
case 1:
420
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);
421
break;
422
case 2:
423
break;
424
default: break;
425
}
426
try
427
{
428
//增加表记录语句
429
// "insert into " + strTableName + " values " + strFieldVaule;PriKey, Description
430
strCmd = " INSERT INTO "+strTableName+" ("+strFieldName+ ") VALUES ("+strFieldVaule+ ")";
431![]()
432
//调用自定义函数,打开 操作 关闭数据库
433
bOperation = OperateDataBase();
434
if (bOperation == true)
435
return true;
436
else
437
return false;
438
}
439
catch
440
{
441
strErrs = "增加记录失败!";
442
SubError(strHostName, strDbName, strSa, "", strErrs);
443
return false; //操作失败,返回flase
444
}
445
finally
446
{
447
//程序运行日志
448
SubLog(strSub);
449
}
450
}
451![]()
452![]()
453
/// <summary>
454
/// 数据库备份
455
/// </summary>
456
/// <param name="strHostName">主机名</param>
457
/// <param name="strSa">数据库用户名</param>
458
/// <param name="strSaPwd">数据库密码</param>
459
/// <param name="strBackupDb">备份数据名</param>
460
/// <param name="strBackPath">备份路径</param>
461
/// <param name="nDbType">数据库类型</param>
462
/// <returns></returns>
463
public bool BackUpDataBase(string strHostName, string strSa, string strSaPwd, string strBackupDb, string strBackPath, int nDbType)
464
{
465
strSub = "BackUpDataBase";
466
//根据输入nDbType判断数据库类型,1为SQL Server ..
467
if (nDbType == 1)
468
{
469
try
470
{
471
string path = @"c:\" + strBackupDb + ".bak";
472
string backupstr = "backup database "+strBackupDb +" to disk='" + path + "';";
473
string strCon = "server="+strHostName +";uid="+strSa +";pwd="+strSaPwd ;
474
// SqlConnection con = new SqlConnection("server=NWAFT6929FES;uid=sa;pwd=121;");
475
SqlConnection con = new SqlConnection(strCon);
476
SqlCommand cmd = new SqlCommand(backupstr, con);
477
con.Open();
478
cmd.ExecuteNonQuery();
479
con.Close();
480
return true;
481
}
482
catch
483
{
484
strErrs = "数据库备份失败!";
485
SubError(strHostName, "", strSa, "", strErrs);
486
return false;
487
}
488
finally
489
{
490
//程序运行日志
491
SubLog(strSub);
492
}
493
494
}
495
else
496
{
497
if (nDbType == 2) //期他数据库类型(预留)
498
{
499
}
500
return false ;
501
}
502
}
503
/// <summary>
504
/// 数据库还原
505
/// </summary>
506
/// <param name="strHostName">主机名</param>
507
/// <param name="strSa">数据库用户名</param>
508
/// <param name="strSaPwd">数据库密码</param>
509
/// <param name="strRestoreDb">还原数据名</param>
510
/// <param name="strRestorePath">还原路径</param>
511
/// <param name="nDbType">数据库类型</param>
512
/// <returns></returns>
513
public bool DbRestore(string strHostName, string strSa, string strSaPwd, string strRestoreDb, string strRestorePath, int nDbType)
514
{
515
strSub = "DbRestore";
516
if (nDbType == 1)
517
{
518
try
519
{
520
string path = @"c:\" + strRestoreDb + ".bak";
521
string restore = "restore database " + strRestoreDb + " from disk='" + path + "';";
522
string strCon = "server=" + strHostName + ";uid=" + strSa + ";pwd=" + strSaPwd;
523
// SqlConnection con = new SqlConnection("server=NWAFT6929FES;uid=sa;pwd=121;");
524
SqlConnection con = new SqlConnection(strCon);
525
SqlCommand cmd = new SqlCommand(restore, con);
526
con.Open();
527
cmd.ExecuteNonQuery();
528
con.Close();
529
return true;
530
}
531
catch
532
{
533
strErrs = "数据库还原失败!";
534
SubError(strHostName, "",strSa, "", strErrs);
535
return false; //操作失败,返回flase
536
}
537
finally
538
{
539
//程序运行日志
540
SubLog(strSub);
541
}
542
}
543
else
544
{
545
if (nDbType == 2) //期他数据库类型(预留)
546
{
547
}
548
return false;
549
}
550
}
551
552
/// <summary>
553
/// 自定义函数,用于生成连接数据库字符串
554
/// </summary>
555
/// <param name="strHostName">主机名</param>
556
/// <param name="strDbName">数据库名</param>
557
/// <param name="strSa">数据库用户名</param>
558
/// <param name="strSaPwd">数据库密码</param>
559
/// <param name="nDbType">数据库类型</param>
560
/// <returns></returns>
561
private string GetStrConnection(string strHostName, string strDbName, string strSa, string strSaPwd, int nDbType)
562
{
563
string strConn = "";
564
switch (nDbType)
565
{
566
case 1:
567
strConn = "Integrated Security=SSPI;Initial Catalog=" + strDbName + ";Data Source=" + strHostName + ";User ID=" + strSa + ";Password=" + strSaPwd + ";";
568
break;
569
case 2:
570
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Password=" + strSa + ";User ID=" + strSaPwd + ";Data Source=" + strHostName;
571
break;
572
case 3: break;
573
default:
574
break;
575
}
576
return strConn;
577
578
}
579
/// <summary>
580
///
581
/// </summary>
582
/// <param name="strHostName"></param>
583
/// <param name="strDbName"></param>
584
/// <param name="strSa"></param>
585
/// <param name="strSaPwd"></param>
586
/// <param name="nDbType"></param>
587
/// <returns></returns>
588
public bool bOpen(string strHostName, string strDbName, string strSa, string strSaPwd, int nDbType)
589
{
590
string conn= GetStrConnection(strHostName,strDbName, strSa, strSaPwd, nDbType);
591
bool b;
592
try
593
{
594
sqlConn = new SqlConnection(conn );
595
sqlConn.Open();
596
if (sqlConn.State.ToString() == "open")
597
return true;
598
else
599
return false;
600
}
601
catch
602
{
603
return false;
604
}
605
finally
606
{
607
}
608![]()
609
}
610
//自定义函数,打开、操作、关闭数据库
611
/// <summary>
612
///
613
/// </summary>
614
/// <returns></returns>
615
private bool OperateDataBase()
616
{
617
strSub = "OperateDataBase";
618
try
619
{
620
sqlConn = new SqlConnection(strConnection); //建立连接对象
621
sqlConn.Open(); //打开连接
622![]()
623
sqlCmd = new SqlCommand(strCmd, sqlConn); //建立操作对象
624
SqlDataReader o;
625
o= sqlCmd.ExecuteReader(); //执行数据库操作sql
626
sqlConn.Close();
627
if (o != null)
628
return true;
629
else return false;
630
//关闭数据库连接
631
632
633
634
}
635
catch
636
{
637
strErrs = "数据库操作失败!";
638
SubError("", "","", "", strErrs);
639
return false ;
640
}
641
finally
642
{
643
//程序运行日志
644
SubLog(strSub);
645
}
646
}
647![]()
648![]()
649
//捕捉错误信息
650
/// <summary>
651
///
652
/// </summary>
653
/// <param name="strHostName"></param>
654
/// <param name="strDbName"></param>
655
/// <param name="strSa"></param>
656
/// <param name="strSaPwd"></param>
657
/// <param name="strErrs"></param>
658
public static void SubError(string strHostName, string strDbName, string strSa, string strSaPwd,string strErrs)
659
{
660
String strErr;
661
strErr = strErrs + "([" + strHostName + "][" + strDbName + "][" + strSa + "][" + strSaPwd + "])";
662
if (bShowMessageBox == false)
663
{
664![]()
665
}
666
else
667
{
668
MessageBox.Show(strErr);
669
}
670![]()
671
//程序运行异常日志
672
SubErrLog(strSub);
673
}
674
//记录系统日志
675
/// <summary>
676
///
677
/// </summary>
678
/// <param name="strSubName"></param>
679
public static void SubLog(string strSubName)
680
{
681
//调用外部接口函数写函数运行过程详细情况(预留)
682
}
683
/// <summary>
684
///
685
/// </summary>
686
/// <param name="strSubName"></param>
687
public static void SubErrLog(string strSubName)
688
{
689
//写函数运行过程异常详细情况(预留)
690![]()
691
}
692![]()
693
}
694
}
695
////////////
696
测试窗体
697![]()
698
using System;
699
using System.Collections.Generic;
700
using System.ComponentModel;
701
using System.Data;
702
using System.Drawing;
703
using System.Text;
704
using System.Windows.Forms;
705
using System.Data.SqlClient;
706![]()
707![]()
708
namespace SqlControl
709
{
710
public partial class Form1 : Form
711
{
712
SoSqlEdit.Sql s = new SoSqlEdit.Sql();
713
public bool b;
714![]()
715
public int recordNum; // 记录数
716![]()
717
public int pageIndex=1; // 目前所在页
718![]()
719
public int pageNum; // 总页数
720![]()
721
public int pageEach = 8; // 每页记录数
722![]()
723![]()
724
public Form1()
725
{
726
InitializeComponent();
727
}
728![]()
729
private void button1_Click(object sender, EventArgs e)
730
{
731
// SoSqlEdit.Sql .UpdaterDB ("DB8E369804344E0","supermap","sa","121",1,"update user1 set pwd='hl520' where name ='lhj'");
732
//MessageBox.Show(SoSqlEdit .Sql .strQueryResult );
733
// this.textBox1.Text = s.UpdaterDB("DB8E369804344E0", "supermap", "sa", "121", "user1", "pwd='tingting530'", "name='lhj'", 1).ToString();
734
b = s.UpdaterDB("localhost", "supermap", "sa", "121", "user1", "pwd='ting'", "name='lhj'", 1);
735
this.textBox1.Text = b.ToString();
736
737
}
738![]()
739
private void Form1_Load(object sender, EventArgs e)
740
{
741![]()
742
}
743![]()
744
private void button2_Click(object sender, EventArgs e)
745
{
746![]()
747
//SoSqlEdit.Sql.QueryDB("DB8E369804344E0", "supermap", "sa", "121", 1, "user1", "name","lhj'");
748
// MessageBox.Show(SoSqlEdit.Sql.strQueryResult);
749![]()
750
/*
751
SqlConnection conn = new SqlConnection("data source=DB8E369804344E0;initial catalog=supermap;User ID=sa;Password=121;");
752
try
753
{
754
conn.Open();// 打开数据库连接
755
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM user1", conn);// 创建数据适配器
756
DataSet ds = new DataSet("user1");// 创建一个数据集对象并填充数据,然后将数据显示在DataGridView控件中
757
da.Fill(ds, "user1");
758
dataGridView1.DataSource = ds.Tables["user1"].DefaultView;
759
// richTextBox1.Text = ds.Tables["user1"].Rows[1].ToString(); ;
760![]()
761
// MessageBox.Show(this, "读取表数据成功。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
762
}
763
catch (Exception Mye)
764
{
765
MessageBox.Show(this, Mye.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
766
}
767
conn.Close(); */
768![]()
769
}
770![]()
771
private void button3_Click(object sender, EventArgs e)
772
{
773
// SoSqlEdit.Sql.CreateDB("localhost",);
774
bool b = s.CreateDB("NWAFT6929FES", "hncu", @"G:\program files\microsoft sql server\mssql\data\", 1);
775
this.textBox5.Text = b.ToString();
776
}
777![]()
778
private void button4_Click(object sender, EventArgs e)
779
{
780
b = s.DelDataBase("localhost", "hncu", 1);
781
this.textBox4.Text = b.ToString();
782
}
783![]()
784
private void button6_Click(object sender, EventArgs e)
785
{
786
b = s.AddTable("localhost", "supermap", "sa", "121", "lll", 1);
787
this.textBox2.Text = b.ToString();
788
}
789![]()
790
private void button5_Click(object sender, EventArgs e)
791
{
792
b = s.DelTable("localhost", "supermap", "sa", "121", "lll", 1);
793
this.textBox3.Text = b.ToString();
794
}
795
private void button7_Click(object sender, EventArgs e)
796
{
797
// SoSqlEdit .Sql
798
bool b = s.BackUpDataBase("localhost", "sa", "121", "supermap", @"c:\", 1);
799
textBox6.Text = b.ToString();
800
}
801![]()
802
private void button8_Click(object sender, EventArgs e)
803
{
804
bool b = s.DbRestore("localhost", "sa", "121", "supermap", @"c:\", 1);
805
textBox7.Text = b.ToString();
806
}
807![]()
808
private void button10_Click(object sender, EventArgs e)
809
{
810
b = s.AddTableField("localhost", "supermap", "sa", "121", "user1", "aa", "char(50)", 1);
811
textBox9.Text = b.ToString();
812
}
813![]()
814
private void button9_Click(object sender, EventArgs e)
815
{
816
b = s.DelTableField("localhost", "supermap", "sa", "121", "user1", "aa", 1);
817
textBox8.Text = b.ToString();
818
}
819![]()
820
private void button11_Click(object sender, EventArgs e)
821
{
822![]()
823
//DataSet ds= s.GetDataSet("DB8E369804344E0", "supermap", "sa", "121","select * from user1",1);
824![]()
825
DataSet ds = s.GetDataSet("localhost", "supermap", "sa", "121", "*", " user1", "name='lhj'", 1);
826
dataGridView1.DataSource = ds.Tables[0].DefaultView;
827
}
828![]()
829
private void button12_Click(object sender, EventArgs e)
830
{
831
// //string s= "select * from openrowset('Microsoft.Jet.OLEDB.4.0', '\\192.168.0.82\\D:\\testMDAC\\mydb.mdb';'admin';'', table_name)";
832
// DataSet ds = new DataSet();
833![]()
834
// string strSQL = "exec GetData '表','字段','条件','排序字段','升序/降序','" + pageEach + "','" + pageIndex + "','0'";
835
// SqlConnection conn = new SqlConnection("data source=localhost;initial catalog=supermap;User ID=sa;Password=121;");
836
// SqlDataAdapter cmd = new SqlDataAdapter(strSQL, conn);
837![]()
838
// cmd.Fill(ds);
839![]()
840
//dataGridView1 .DataSource = ds.Tables[0].DefaultView;
841![]()
842
s.bOpen("localhost", "supermap", "sa", "121",1);
843
}
844![]()
845
private void button2_Click_1(object sender, EventArgs e)
846
{
847
b= s.AddRecord("localhost", "supermap", "sa", "121", " user1", "name,pwd,RuleID","'我啊','1478','1'", 1);
848
this.textBox10.Text = b.ToString();
849
}
850![]()
851
852
}
853
}
类中 soSqlEdit.cs2
//########################################################################3
// 目的: 新建和删除常用数据库: CreateDB/DelDB4
// 新建和删除常用数据库中的表: AddTable/DelTable5
// 新建和删除常用数据库中表的字段: AddTableField:/DelTableField6
// 对数据库进行备份和还原: BackUpDataBase/DbRestore7
// 对数据库字段值进行修改: UpdaterDB8
// 对数据库字段值进行查询: GetDataSet9

10
// 输入: strHostName: 主机名11
// strDbName: 数据库名12
// strSa: 服务器登录名13
// strSaPwd: 服务器登录密码14
// nDbType : 连接的数据库类型 (1:Miscrosoft SQL server)15
// strSqlUpdate: 数据库操作字符串16

17
// 返回: 返回查询结果,并以数据集显示18
//########################################################################19

20

21

22
using System;23
using System.Collections.Generic;24
using System.Text;25
using System.Data;26
using System.Data.SqlClient;27
using System.Windows.Forms;28
namespace SoSqlEdit29
{ 30
public class Sql31
{32
/// <summary>33
/// 34
/// </summary>35
public static string strSub; //程序过程函数名36
/// <summary>37
/// 38
/// </summary>39
public static string strCls = "ControlIni"; //代码文件名40

41
public static string strErrs; //运行说明 42
public static Boolean bShowMessageBox = false; //是否弹出错误提示框(默认不弹出)43
bool bOperation;44

45
public string strConnection; //连接字符串46
public string strCmd; //操作字符串47
48

49
public SqlConnection sqlConn; //连接对象50
public SqlCommand sqlCmd; //操作对象 51
DataSet ds = new DataSet(); //记录集对象52
/// <summary>53
/// 对数据库更新54
/// </summary>55
/// <param name="strHostName">主机名</param>56
/// <param name="strDbName">数据库名</param>57
/// <param name="strSa">数据库用户名</param>58
/// <param name="strSaPwd">数据库登录密码</param>59
/// <param name="strTableName">表名</param>60
/// <param name="strUpdater">修改字段</param>61
/// <param name="strReson">修改条件</param>62
/// <param name="nDbType">数据库类型</param>63
/// <returns></returns>64
public bool UpdaterDB(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName,string strUpdater,string strReson,int nDbType)65
{ 66
switch (nDbType)67
{68
case 1:69
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);70
break;71
case 2:72
break;73
default:74
break;75
}76
try77
{78
//建立数据库操作语句79
strCmd = "update " + strTableName + " set "+strUpdater +" where " + strReson;80
//调用自定义函数,打开 操作 关闭数据库81
bOperation = OperateDataBase();82
if (bOperation == true)83
return true; //操作成功,返回真 84
else85
return false;86
}87
catch88
{89
return false; //操作失败,返回flase90
}91
finally92
{93

94
}95
96
}97

98
//99
/// <summary>100
/// 根据条件得到查询结果,返回数据集101
/// </summary>102
/// <param name="strHostName">主机名</param>103
/// <param name="strDbName">数据库名</param>104
/// <param name="strSa">数据库用户名</param>105
/// <param name="strSaPwd">数据库登录密码</param>106
/// <param name="strResult">查询列名</param>107
/// <param name="strTableName">表名</param>108
/// <param name="strReson">查询条件</param>109
/// <param name="nDbType">数据库类型</param>110
/// <returns></returns>111
public DataSet GetDataSet(string strHostName, string strDbName, string strSa, string strSaPwd, string strResult, string strTableName, string strReson, int nDbType)112
{113
switch (nDbType)114
{115
case 1:116
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);117
break;118
case 2:119
break;120
default: break;121
}122
SqlConnection conn = new SqlConnection(strConnection);123
try124
{125
string strFind = "select " + strResult + " from " + strTableName + " where " + strReson;126
conn.Open();// 打开数据库连接127
SqlDataAdapter da = new SqlDataAdapter(strFind, conn);// 创建数据适配器128
da.Fill(ds);129
return ds;130
}131
catch132
{133
return null;134
}135
finally136
{137
conn.Close();138
}139

140
}141

142
/// <summary>143
/// 建立数据库144
/// </summary>145
/// <param name="strHostName">主机名</param>146
/// <param name="strNewDbName">数据库名</param>147
/// <param name="strDbPath">路径名</param>148
/// <param name="nDbType">数据库类型</param>149
/// <returns></returns>150
public bool CreateDB(string strHostName, string strNewDbName, string strDbPath, int nDbType)151
{152
switch (nDbType)153
{ 154
case 1:155
strConnection = GetStrConnection(strHostName,"","","",1);156
break;157
case 2:158
strConnection = GetStrConnection(strHostName, "", "", "", 2);159
break;160
default: break;161
} 162
try163
{164
strCmd = "CREATE DATABASE " + strNewDbName + " ON PRIMARY" + "(name=" + strNewDbName + ", filename ='" + strDbPath + strNewDbName + ".mdf', size=3," + "maxsize=5, filegrowth=10%)";165
//调用自定义函数,打开 操作 关闭数据库 166
bOperation = OperateDataBase();167
if (bOperation == true)168
return true;169
else170
return false;171
}172
catch173
{174
return false; //操作失败,返回flase175
}176
finally177
{178
}179
}180
181
/// <summary>182
/// 删除指定数据库183
/// </summary>184
/// <param name="strHostName">主机名</param>185
/// <param name="strDbName">数据库名</param>186
/// <param name="nDbType">数据库类型</param>187
/// <returns></returns>188
public bool DelDataBase(string strHostName, string strDbName, int nDbType)189
{190
switch (nDbType)191
{192
case 1:193
strConnection = GetStrConnection(strHostName,"","","",1);194
break;195
case 2:196
break;197
default :198
break;199
}200
try201
{202
strCmd = "drop database " + strDbName; //建立操作字符串203
bOperation = OperateDataBase();204
if (bOperation == true)205
return true;206
else207
return false;208
}209
catch 210
{211
return false; //操作失败,返回flase212
}213
finally 214
{215
}216
}217
//218
/// <summary>219
/// 向数据库中添加表220
/// </summary>221
/// <param name="strHostName">主机名</param>222
/// <param name="strDbName">数据库名</param>223
/// <param name="strSa">数据库用户名</param>224
/// <param name="strSaPwd">数据库登录密码</param>225
/// <param name="strTableName">表名</param>226
/// <param name="nDbType">数据库类型</param>227
/// <returns></returns>228
public bool AddTable(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName, int nDbType)229
{230
strSub = "AddTable";231
switch (nDbType)232
{233
case 1:234
strConnection = GetStrConnection(strHostName,strDbName, strSa, strSaPwd, nDbType);235
break;236
case 2: break;237
default: break;238
}239
try240
{241
//建立表操作语句242
strCmd = "CREATE TABLE "+strTableName + "(id char(50))";243
//调用自定义函数,打开 操作 关闭数据库244
bOperation = OperateDataBase();245
if (bOperation == true)246
return true;247
else248
return false;249
}250

251
catch252
{253
strErrs = "添加数据库的表失败!";254
SubError(strHostName, strDbName, strSa, "", strErrs);255
return false; //操作失败,返回flase256
} 257
finally258
{259
//程序运行日志260
SubLog(strSub);261
}262
}263
//264
/// <summary>265
/// 删除数据库中表266
/// </summary>267
/// <param name="strHostName">主机名</param>268
/// <param name="strDbName">数据库名</param>269
/// <param name="strSa">数据库用户名</param>270
/// <param name="strSaPwd">数据库登录密码</param>271
/// <param name="strTableName">表名</param>272
/// <returns></returns>273
public bool DelTable(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName, int nDbType)274
{275
strSub = "DelTable";276
switch (nDbType)277
{278
case 1:279
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);280
break;281
default :break ;282
}283
try284
{285
//删除数据库表操作语句286
strCmd = "DROP TABLE " + strTableName;287
//调用自定义函数,打开 操作 关闭数据库288
bOperation = OperateDataBase();289
if (bOperation == true)290
return true;291
else292
return false;293
}294

295
catch296
{297
strErrs = "删除数据库的表失败!";298
SubError(strHostName, strDbName, strSa, "", strErrs);299
return false; //操作失败,返回flase300
}301
finally302
{303
//程序运行日志304
SubLog(strSub);305
}306
}307
308
/// <summary>309
/// 向数据库中添加字段值310
/// </summary>311
/// <param name="strHostName">主机名</param>312
/// <param name="strDbName">数据库名</param>313
/// <param name="strSa">数据库用户名</param>314
/// <param name="strSaPwd">数据库登录密码</param>315
/// <param name="strTableName">表名</param>316
/// <param name="strFieldName">字段名</param>317
/// <param name="strFieldType">字段类型</param>318
/// <param name="nDbType">数据库类型</param>319
/// <returns></returns>320
public bool AddTableField(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName,string strFieldName,string strFieldType, int nDbType)321
{322
strSub = "AddTableField";323
switch (nDbType)324
{325
case 1:326
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);327
break;328
default: break;329
}330
try331
{332
//增加表字段操作语句333
strCmd = "ALTER TABLE "+strTableName +" ADD "+strFieldName +" "+ strFieldType ;334
//调用自定义函数,打开 操作 关闭数据库335
bOperation = OperateDataBase();336
if (bOperation == true)337
return true;338
else339
return false;340
}341

342
catch343
{344
strErrs = "添加数据库字段失败!";345
SubError(strHostName, strDbName, strSa, "", strErrs);346
return false; //操作失败,返回flase347
}348
finally349
{ 350
//程序运行日志351
SubLog(strSub);352

353
}354
}355
//356
/// <summary>357
/// 删除数据库中指定字段值358
/// </summary>359
/// <param name="strHostName">主机名</param>360
/// <param name="strDbName">数据库名</param>361
/// <param name="strSa">数据库用户名</param>362
/// <param name="strSaPwd">数据库登录密码</param>363
/// <param name="strTableName">表名</param>364
/// <param name="strFieldName">字段名</param>365
/// <param name="nDbType">数据库类型</param>366
/// <returns></returns>367
public bool DelTableField(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName, string strFieldName, int nDbType)368
{369
strSub = "DelTableField";370
switch (nDbType)371
{372
case 1:373
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);374
break;375
case 2:376
break;377
default: break;378
}379
try380
{381
//删除表字段操作语句382
strCmd = "alter table "+strTableName + " drop column "+strFieldName;383
//调用自定义函数,打开 操作 关闭数据库384
bOperation = OperateDataBase();385
if (bOperation == true)386
return true;387
else388
return false;389
}390
catch//(Exception Ex) throw Ex; 391
{392
strErrs = "删除字段失败!";393
SubError(strHostName, strDbName, strSa, "", strErrs);394
return false; //操作失败,返回flase395
}396
finally397
{ 398
//程序运行日志399
SubLog(strSub);400
}401
}402
/// <summary>403
/// 404
/// </summary>405
/// <param name="strHostName"></param>406
/// <param name="strDbName"></param>407
/// <param name="strSa"></param>408
/// <param name="strSaPwd"></param>409
/// <param name="strTableName"></param>410
/// <param name="strFieldName"></param>411
/// <param name="strFieldVaule"></param>412
/// <param name="nDbType"></param>413
/// <returns></returns>414
public bool AddRecord(string strHostName, string strDbName, string strSa, string strSaPwd, string strTableName, string strFieldName,string strFieldVaule, int nDbType)415
{416
strSub = "AddRecord";417
switch (nDbType)418
{419
case 1:420
strConnection = GetStrConnection(strHostName, strDbName, strSa, strSaPwd, nDbType);421
break;422
case 2:423
break;424
default: break;425
}426
try427
{428
//增加表记录语句429
// "insert into " + strTableName + " values " + strFieldVaule;PriKey, Description430
strCmd = " INSERT INTO "+strTableName+" ("+strFieldName+ ") VALUES ("+strFieldVaule+ ")";431

432
//调用自定义函数,打开 操作 关闭数据库433
bOperation = OperateDataBase();434
if (bOperation == true)435
return true;436
else437
return false;438
}439
catch440
{441
strErrs = "增加记录失败!";442
SubError(strHostName, strDbName, strSa, "", strErrs);443
return false; //操作失败,返回flase444
}445
finally446
{447
//程序运行日志448
SubLog(strSub);449
}450
}451

452

453
/// <summary>454
/// 数据库备份455
/// </summary>456
/// <param name="strHostName">主机名</param>457
/// <param name="strSa">数据库用户名</param>458
/// <param name="strSaPwd">数据库密码</param>459
/// <param name="strBackupDb">备份数据名</param>460
/// <param name="strBackPath">备份路径</param>461
/// <param name="nDbType">数据库类型</param>462
/// <returns></returns>463
public bool BackUpDataBase(string strHostName, string strSa, string strSaPwd, string strBackupDb, string strBackPath, int nDbType)464
{465
strSub = "BackUpDataBase";466
//根据输入nDbType判断数据库类型,1为SQL Server ..467
if (nDbType == 1)468
{469
try470
{471
string path = @"c:\" + strBackupDb + ".bak";472
string backupstr = "backup database "+strBackupDb +" to disk='" + path + "';";473
string strCon = "server="+strHostName +";uid="+strSa +";pwd="+strSaPwd ;474
// SqlConnection con = new SqlConnection("server=NWAFT6929FES;uid=sa;pwd=121;");475
SqlConnection con = new SqlConnection(strCon);476
SqlCommand cmd = new SqlCommand(backupstr, con);477
con.Open();478
cmd.ExecuteNonQuery();479
con.Close();480
return true;481
}482
catch483
{484
strErrs = "数据库备份失败!";485
SubError(strHostName, "", strSa, "", strErrs);486
return false;487
}488
finally489
{490
//程序运行日志491
SubLog(strSub);492
} 493
494
}495
else496
{497
if (nDbType == 2) //期他数据库类型(预留)498
{499
}500
return false ;501
} 502
}503
/// <summary>504
/// 数据库还原505
/// </summary>506
/// <param name="strHostName">主机名</param>507
/// <param name="strSa">数据库用户名</param>508
/// <param name="strSaPwd">数据库密码</param>509
/// <param name="strRestoreDb">还原数据名</param>510
/// <param name="strRestorePath">还原路径</param>511
/// <param name="nDbType">数据库类型</param>512
/// <returns></returns>513
public bool DbRestore(string strHostName, string strSa, string strSaPwd, string strRestoreDb, string strRestorePath, int nDbType)514
{515
strSub = "DbRestore";516
if (nDbType == 1)517
{518
try519
{520
string path = @"c:\" + strRestoreDb + ".bak";521
string restore = "restore database " + strRestoreDb + " from disk='" + path + "';";522
string strCon = "server=" + strHostName + ";uid=" + strSa + ";pwd=" + strSaPwd;523
// SqlConnection con = new SqlConnection("server=NWAFT6929FES;uid=sa;pwd=121;");524
SqlConnection con = new SqlConnection(strCon);525
SqlCommand cmd = new SqlCommand(restore, con);526
con.Open();527
cmd.ExecuteNonQuery();528
con.Close();529
return true;530
}531
catch532
{533
strErrs = "数据库还原失败!";534
SubError(strHostName, "",strSa, "", strErrs);535
return false; //操作失败,返回flase536
}537
finally538
{539
//程序运行日志540
SubLog(strSub);541
}542
}543
else544
{545
if (nDbType == 2) //期他数据库类型(预留)546
{547
}548
return false;549
}550
}551
552
/// <summary>553
/// 自定义函数,用于生成连接数据库字符串554
/// </summary>555
/// <param name="strHostName">主机名</param>556
/// <param name="strDbName">数据库名</param>557
/// <param name="strSa">数据库用户名</param>558
/// <param name="strSaPwd">数据库密码</param>559
/// <param name="nDbType">数据库类型</param>560
/// <returns></returns>561
private string GetStrConnection(string strHostName, string strDbName, string strSa, string strSaPwd, int nDbType)562
{563
string strConn = "";564
switch (nDbType)565
{566
case 1:567
strConn = "Integrated Security=SSPI;Initial Catalog=" + strDbName + ";Data Source=" + strHostName + ";User ID=" + strSa + ";Password=" + strSaPwd + ";";568
break;569
case 2:570
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Password=" + strSa + ";User ID=" + strSaPwd + ";Data Source=" + strHostName;571
break;572
case 3: break;573
default:574
break;575
}576
return strConn;577
578
}579
/// <summary>580
/// 581
/// </summary>582
/// <param name="strHostName"></param>583
/// <param name="strDbName"></param>584
/// <param name="strSa"></param>585
/// <param name="strSaPwd"></param>586
/// <param name="nDbType"></param>587
/// <returns></returns>588
public bool bOpen(string strHostName, string strDbName, string strSa, string strSaPwd, int nDbType)589
{590
string conn= GetStrConnection(strHostName,strDbName, strSa, strSaPwd, nDbType);591
bool b;592
try593
{594
sqlConn = new SqlConnection(conn );595
sqlConn.Open();596
if (sqlConn.State.ToString() == "open")597
return true;598
else599
return false;600
}601
catch602
{603
return false;604
}605
finally606
{607
}608

609
}610
//自定义函数,打开、操作、关闭数据库611
/// <summary>612
/// 613
/// </summary>614
/// <returns></returns>615
private bool OperateDataBase()616
{617
strSub = "OperateDataBase";618
try619
{620
sqlConn = new SqlConnection(strConnection); //建立连接对象 621
sqlConn.Open(); //打开连接 622

623
sqlCmd = new SqlCommand(strCmd, sqlConn); //建立操作对象624
SqlDataReader o;625
o= sqlCmd.ExecuteReader(); //执行数据库操作sql626
sqlConn.Close(); 627
if (o != null)628
return true;629
else return false;630
//关闭数据库连接631
632
633
634
}635
catch636
{637
strErrs = "数据库操作失败!";638
SubError("", "","", "", strErrs);639
return false ;640
}641
finally642
{643
//程序运行日志644
SubLog(strSub);645
}646
}647

648

649
//捕捉错误信息650
/// <summary>651
/// 652
/// </summary>653
/// <param name="strHostName"></param>654
/// <param name="strDbName"></param>655
/// <param name="strSa"></param>656
/// <param name="strSaPwd"></param>657
/// <param name="strErrs"></param>658
public static void SubError(string strHostName, string strDbName, string strSa, string strSaPwd,string strErrs)659
{660
String strErr;661
strErr = strErrs + "([" + strHostName + "][" + strDbName + "][" + strSa + "][" + strSaPwd + "])";662
if (bShowMessageBox == false)663
{664

665
}666
else667
{668
MessageBox.Show(strErr);669
}670

671
//程序运行异常日志672
SubErrLog(strSub);673
}674
//记录系统日志675
/// <summary>676
/// 677
/// </summary>678
/// <param name="strSubName"></param>679
public static void SubLog(string strSubName)680
{681
//调用外部接口函数写函数运行过程详细情况(预留)682
}683
/// <summary>684
/// 685
/// </summary>686
/// <param name="strSubName"></param>687
public static void SubErrLog(string strSubName)688
{689
//写函数运行过程异常详细情况(预留)690

691
}692

693
}694
}695
////////////696
测试窗体697

698
using System;699
using System.Collections.Generic;700
using System.ComponentModel;701
using System.Data;702
using System.Drawing;703
using System.Text;704
using System.Windows.Forms;705
using System.Data.SqlClient;706

707

708
namespace SqlControl709
{710
public partial class Form1 : Form711
{712
SoSqlEdit.Sql s = new SoSqlEdit.Sql();713
public bool b;714

715
public int recordNum; // 记录数 716

717
public int pageIndex=1; // 目前所在页 718

719
public int pageNum; // 总页数 720

721
public int pageEach = 8; // 每页记录数 722

723

724
public Form1()725
{726
InitializeComponent();727
}728

729
private void button1_Click(object sender, EventArgs e)730
{731
// SoSqlEdit.Sql .UpdaterDB ("DB8E369804344E0","supermap","sa","121",1,"update user1 set pwd='hl520' where name ='lhj'");732
//MessageBox.Show(SoSqlEdit .Sql .strQueryResult );733
// this.textBox1.Text = s.UpdaterDB("DB8E369804344E0", "supermap", "sa", "121", "user1", "pwd='tingting530'", "name='lhj'", 1).ToString();734
b = s.UpdaterDB("localhost", "supermap", "sa", "121", "user1", "pwd='ting'", "name='lhj'", 1);735
this.textBox1.Text = b.ToString();736
737
}738

739
private void Form1_Load(object sender, EventArgs e)740
{741

742
}743

744
private void button2_Click(object sender, EventArgs e)745
{746

747
//SoSqlEdit.Sql.QueryDB("DB8E369804344E0", "supermap", "sa", "121", 1, "user1", "name","lhj'");748
// MessageBox.Show(SoSqlEdit.Sql.strQueryResult);749

750
/*751
SqlConnection conn = new SqlConnection("data source=DB8E369804344E0;initial catalog=supermap;User ID=sa;Password=121;");752
try753
{754
conn.Open();// 打开数据库连接755
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM user1", conn);// 创建数据适配器756
DataSet ds = new DataSet("user1");// 创建一个数据集对象并填充数据,然后将数据显示在DataGridView控件中757
da.Fill(ds, "user1");758
dataGridView1.DataSource = ds.Tables["user1"].DefaultView;759
// richTextBox1.Text = ds.Tables["user1"].Rows[1].ToString(); ;760

761
// MessageBox.Show(this, "读取表数据成功。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);762
}763
catch (Exception Mye)764
{765
MessageBox.Show(this, Mye.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);766
}767
conn.Close(); */768

769
}770

771
private void button3_Click(object sender, EventArgs e)772
{773
// SoSqlEdit.Sql.CreateDB("localhost",);774
bool b = s.CreateDB("NWAFT6929FES", "hncu", @"G:\program files\microsoft sql server\mssql\data\", 1);775
this.textBox5.Text = b.ToString();776
}777

778
private void button4_Click(object sender, EventArgs e)779
{780
b = s.DelDataBase("localhost", "hncu", 1); 781
this.textBox4.Text = b.ToString();782
}783

784
private void button6_Click(object sender, EventArgs e)785
{786
b = s.AddTable("localhost", "supermap", "sa", "121", "lll", 1);787
this.textBox2.Text = b.ToString();788
}789

790
private void button5_Click(object sender, EventArgs e)791
{792
b = s.DelTable("localhost", "supermap", "sa", "121", "lll", 1);793
this.textBox3.Text = b.ToString();794
}795
private void button7_Click(object sender, EventArgs e)796
{797
// SoSqlEdit .Sql 798
bool b = s.BackUpDataBase("localhost", "sa", "121", "supermap", @"c:\", 1);799
textBox6.Text = b.ToString();800
}801

802
private void button8_Click(object sender, EventArgs e)803
{804
bool b = s.DbRestore("localhost", "sa", "121", "supermap", @"c:\", 1);805
textBox7.Text = b.ToString();806
}807

808
private void button10_Click(object sender, EventArgs e)809
{810
b = s.AddTableField("localhost", "supermap", "sa", "121", "user1", "aa", "char(50)", 1);811
textBox9.Text = b.ToString();812
}813

814
private void button9_Click(object sender, EventArgs e)815
{816
b = s.DelTableField("localhost", "supermap", "sa", "121", "user1", "aa", 1);817
textBox8.Text = b.ToString();818
}819

820
private void button11_Click(object sender, EventArgs e)821
{822

823
//DataSet ds= s.GetDataSet("DB8E369804344E0", "supermap", "sa", "121","select * from user1",1);824

825
DataSet ds = s.GetDataSet("localhost", "supermap", "sa", "121", "*", " user1", "name='lhj'", 1);826
dataGridView1.DataSource = ds.Tables[0].DefaultView; 827
}828

829
private void button12_Click(object sender, EventArgs e)830
{831
// //string s= "select * from openrowset('Microsoft.Jet.OLEDB.4.0', '\\192.168.0.82\\D:\\testMDAC\\mydb.mdb';'admin';'', table_name)";832
// DataSet ds = new DataSet();833

834
// string strSQL = "exec GetData '表','字段','条件','排序字段','升序/降序','" + pageEach + "','" + pageIndex + "','0'";835
// SqlConnection conn = new SqlConnection("data source=localhost;initial catalog=supermap;User ID=sa;Password=121;");836
// SqlDataAdapter cmd = new SqlDataAdapter(strSQL, conn);837

838
// cmd.Fill(ds);839

840
//dataGridView1 .DataSource = ds.Tables[0].DefaultView; 841

842
s.bOpen("localhost", "supermap", "sa", "121",1);843
}844

845
private void button2_Click_1(object sender, EventArgs e)846
{847
b= s.AddRecord("localhost", "supermap", "sa", "121", " user1", "name,pwd,RuleID","'我啊','1478','1'", 1);848
this.textBox10.Text = b.ToString();849
}850

851
852
}853
}



浙公网安备 33010602011771号