直接上代码,参考了网上的类并且做了一些修改和一个简单的demo
以下是SQLHelper类,只需要修改connStrFromXML这个函数改变连接即可直接使用。关于详细使用的写了一个demo!
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Windows.Forms;
using System.Data.Common;
using System.Linq;
namespace SqlHelper
{
public class SQLHelper
{
public SQLHelper()
{
}
public SQLHelper(string connectionStr)
{
connStr = connectionStr;
}
private string connStr;
public string ConnStr
{
get
{
if (connStr == null) connStr = connStrFromXML();
return connStr;
}
}
private SqlConnection connect = null;
public SqlConnection getConnect()
{
if (string.IsNullOrEmpty(connStr))
{
connStr = connStrFromXML();
}
// if (connect == null || connect.State == ConnectionState.Closed)
connect = new SqlConnection(connStr);
return connect;
}
//输入连接信息
private string connStrFromXML()
{
string connStr = "", sname = "", dbname = "", user = "", pwd = "";