c#和mysql连接问题
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using MySQLDriverCS; /** * @author X-H * */ public class DB { MySQLConnection con = null;
MySQLCommand cmd =new MySQLCommand(); public DB() { con = new MySQLConnection(new MySQLConnectionString("localhost", "business", "root", "").AsString); con.Open(); } public MySQLConnection getCon() { if (con.State == ConnectionState.Closed) con.Open();【报错:未将对象引用设置到对象的实例。】 return con; } public void close() { if (con.State == ConnectionState.Open) con.Close(); } public int executeNonQuery(string strSql) { cmd.Connection = getCon(); cmd.CommandText = strSql; int i = Convert.ToInt32(cmd.ExecuteNonQuery()); close(); return i; } public string executeGetReturn(string strSql) { cmd.Connection = getCon();// cmd.CommandText = strSql; string result = cmd.ExecuteScalar().ToString(); close(); return result; } public DataSet getDs(string strSql, string tableName) { MySQLDataAdapter sda = new MySQLDataAdapter(strSql, getCon()); DataSet ds = new DataSet(); sda.Fill(ds, tableName); close(); return ds; } }