本文为Visual C#使用ado.net技术跟SQL Server数据库交互的自定义类MyDBase,通过该类,可以快速高效的实现数据库的连接、获取数据集、获取数据的个数、执行SQL语句等。例如,连接数据库的方法构建为:
public MyDBase(string MyDBServerName, string MyDataBaseName, string sUerName, string sPasswd){ECode = false;string sConn = "workstation id=" + MyDBServerName + ";packet size=4096;user id=" + sUerName + ";pwd=" + sPasswd + ";data source=" + MyDBServerName + ";persist security info=False;initial catalog=" + MyDataBaseName;cn.ConnectionString = sConn;try{cn.Open();}catch (Exception e){ES = e.Message;ECode = true;}}
连接数据库实例:
MyDBase DB=new MyDBase("localhost","TESTDB","sa","Admin");DB.DBC