程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 新近寫了個通用的數據庫訪問類(c#)

新近寫了個通用的數據庫訪問類(c#)

編輯:.NET實例教程

新近寫了個通用的數據庫訪問類(c#);支持oledb\sql\Oracle\odbc數據連接,
提供sysbase,db2 的接口支持,
如果有什麼錯誤、意見、建議。請聯系我[email protected]

http://mail.gongyi.gov.cn/netdisk/[email protected]&id=2712a90f0de399014bfbea6e697381a3&filename=L7mk1/cvTXlQRE8ucmFy&language=gb
經過不完全測試:
測試代碼:
using System;
using System.Collections;
using System.IO;

using NUnit.Framework;
using MyPDO.DataAccess;

namespace DataAccessTest
{
/// <summary>
/// Class1 的摘要說明。
/// </summary>
[TestFixture]
public class DataAccessTest
{
public DataAccessTest()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
private MyPDO.DataAccess.DataAccessor DBA;

[SetUp]
public void SetUp()
{
DBA = new DataAccessor();
DBA.ConnectString = "Provider=SQLOLEDB;data source=10.10.25.63;initial catalog=Master;user id=zha;PassWord=123;";
DBA.ConnectType = MyPDO.ConnectType.OleDbDataDriver;
DBA.Open();
Assert.IsTrue(DBA.IsOpen,"DBA closed");
}
[Test]
public void ExecuteNonQueryTest()
{
Hashtable hsTable = new Hashtable();
hsTable.Add("A","select * from myfiles");
DBA.
Assert.IsTrue(DBA.IsOpen,"DBA closed");
if (DBA.IsOpen == true)
{
DBA.ExecuteNonQuery(hsTable);
}
}
[Test]
public void GetDataSetTest()
{
System.Data.DataSet ds = new System.Data.DataSet();
Assert.IsTrue(DBA.IsOpen,"DBA closed");
if (DBA.IsOpen == true)
{
DBA.CommandText = "select * from myfiles";
ds = DBA.GetDataSet();
}
}
[Test]
public void GetDataTableTest()
{
Assert.IsTrue(DBA.IsOpen,"DBA closed");
if (DBA.IsOpen == true)
{
DBA.CommandText = "select * from myfiles";
System.Data.DataTable dts = DBA.GetDataTable();
}
}
[Test]
public void GetDataReaderTest()
{
Assert.IsTrue(DBA.IsOpen,"DBA closed");
if (DBA.IsOpen == true)
{
DBA.CommandText = "select * from myfiles";
System.Data.IDataReader dtReader = DBA.GetDataReader();
dtReader.Close();
}
}
[Test]
public void ExecuteCommandTest()
{
Assert.IsTrue(DBA.IsOpen,"DBA closed");
if (DBA.IsOpen == true)
{
DBA.CommandText = "select * from myfiles";
DBA.ExecuteCommand(false);
}
}
[Test]
public void SetCommandParametersTest()
{
System.Data.OleDb.OleDbParameter parms = new System.Data.OleDb.OleDbParameter();
parms.ParameterName = "@path";
parms.OleDbType = System.Data.OleDb.OleDbType.VarChar;
parms.Size = 255;
parms.SourceColumn = "path";
parms.Value = "222";

System.Data.OleDb.OleDbParameter parms1 = new System.Data.OleDb.OleDbParameter();
parms1.ParameterName = "@fname";
parms1.OleDbType = System.Data.OleDb.OleDbType.VarChar;
parms1.Size = 255;
parms1.SourceColumn = "fname";
parms1.Value = "1";

MyPDO.DataAccess.DataAccessor DBA = new DataAccessor();
DBA.ConnectString = "Provider=SQLOLEDB;data source=10.10.25.63;initial catalog=Master;user id=zha;PassWord=123;";
DBA.Open();
if (DBA.IsOpen == true)
{
DBA.CommandText = "select * from myfiles where path = ? and fname = ?";
DBA.SetCommandParameter(parms,false);
DBA.SetCommandParameter(parms1,false);
System.Data.DataSet ds = DBA.GetDataSet();
Assert.AreEqual(1,ds.Tables[0].Rows.Count);
DBA.Close();
}
}
[Test]
public void AllUseTest()
{
this.ExecuteCommandTest();
this.ExecuteNonQueryTest();
this.GetDataReaderTest();
this.GetDataReaderTestWithParamBlob();
this.GetDataReaderTestWithParamString();
this.GetDataSetTest();
this.GetDataTableTest();
this.SetCommandParametersTest();
}
[Test]
public void GetDataReaderTestWithParamString()
{
System.Data.OleDb.OleDbParameter parms = new System.Data.OleDb.OleDbParameter();
parms.ParameterName = "@path";
//parms.OleDbType = System.Data.OleDb.OleDbType.VarChar;
//parms.Size = 255;
//parms.SourceColumn = "path";
parms.Value = "eeee";

System.Data.OleDb.OleDbParameter parms1 = new System.Data.OleDb.OleDbParameter();
parms1.ParameterName = "@fname";
//parms1.OleDbType = System.Data.OleDb.OleDbType.VarChar;
//parms1.Size = 255;
//parms1.SourceColumn = "fname";
parms1.Value = "1";

Assert.IsTrue(DBA.IsOpen,"DBA closed");
{
DBA.CommandText = "select * from myfiles where path = ? and fname = ?";

DBA.SetCommandParameter(parms,false);
DBA.SetCommandParameter(parms1,false);

System.Data.IDataReader dtReader = DBA.GetDataReader();
dtReader.Read();
string strPath = dtReader.GetString(0);
Assert.AreEqual("eeee",strPath);
dtReader.Close();
}
}
[Test]
public void GetDataReaderTestWithParamBlob()
{
System.Data.OleDb.OleDbParameter parms = new System.Data.OleDb.OleDbParameter();
parms.ParameterName = "@path";
//parms.OleDbType = System.Data.OleDb.OleDbType.Numeric;
//parms.Size = 8;
//parms.SourceColumn = "c";
parms.Value = 999;

System.Data.OleDb.OleDbParameter parms1 = new System.Data.OleDb.OleDbParameter();
parms1.ParameterName = "@fname";
//parms1.OleDbType = System.Data.OleDb.OleDbType.Binary;
//parms1.Size = 16;
//parms1.SourceColumn = "a";
parms1.Value = GetPhoto(@"c:\a.jpg");
//parms1.Value = "111111111111111111111";

//System.Data.OleDb.OleDbParameter parms2 = new System.Data.OleDb.OleDbParameter()

Assert.IsTrue(DBA.IsOpen,"DBA closed");
{
DBA.CommandText = "insert into test(c,a) values(convert(money,?),?) ";

//DBA.CommandText = "insert into myfiles(path,fname) values(?,?)";
DBA.SetCommandParameter(parms,false);
DBA.SetCommandParameter(parms1,false);

DBA.ExecuteCommand(true);
}
}
public static byte[] GetPhoto(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

byte[] photo = br.ReadBytes((int)fs.Length);

br.Close();
fs.Close();

return photo;
}
[Test]
public void ExecuteStoredProcedureTestOneValue()
{

if (DBA.IsOpen == true)
{
DBA.CommandText = "exec get_2 '222'";
object j = DBA.ExecuteStoredProcedure(MyPDO.StoredProcedureType.OneValue);
string s = (string)j;
Assert.AreEqual("222",s);
}
}
[Test]
public void ExecuteStoredProcedureTestNone()
{
if (DBA.IsOpen == true)
{
DBA.CommandText = "exec get_2 '222'";
object s = DBA.ExecuteStoredProcedure(MyPDO.StoredProcedureType.None);
Assert.AreEqual(null,s);
}
else
Assert.AreEqual(null,"1");
}
[Test]
public void ExecuteStoredProcedureTestDataSet()
{
if (DBA.IsOpen == true)
{
DBA.CommandText = "exec get_2 'ddddd'";
object s = DBA.ExecuteStoredProcedure(MyPDO.StoredProcedureType.DataSet);

System.Data.DataSet ds = (System.Data.DataSet)s;

if(ds.Tables.Count > 0)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt = ds.Tables[0];
int str = dt.Rows.Count;
Assert.AreEqual(3,str);
}
else
Assert.AreEqual(null,"s");
}
else
Assert.AreEqual(null,"1");
}
}
}




作者Blog:http://blog.csdn.Net/yu_nan88/

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved