问题遇到的现象:SQLServer2008链接数据库后并点击登录按钮后系统崩溃
代码如下
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SuperMarketMS
{
/// <summary>
/// 用途:用于直接与数据库进行交互
/// </summary>
public class DBHelper
{
//定义一个静态全局的数据库连接
public static SqlConnection conn;
//初始化数据库连接
public static void GetConnection()
{
try
{
StreamReader sw = new StreamReader(Application.StartupPath + "\\DataSource.txt");
string connString = sw.ReadToEnd();
sw.Close();
conn = new SqlConnection(connString);
}
catch
{
;//DataSource.txt.不存在时
}
}
#region 返回单行单列的值,用于登录验证
public static int IsExecute(string sql)
{
int count = 0;
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
count = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
return count;
}
#endregion
#region 执行增删改的方法
public static int UserInsertDeleteUpdate(string sql)
{
int count = 0;
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
count = Convert.ToInt32(cmd.ExecuteNonQuery());
conn.Close();
return count;
}
#endregion
//------------------------------------------------------------------------
public static DataSet ExecuteDataset(string cmdText, CommandType cmdType, string TableName, params SqlParameter[] cmdParms)
{
if (conn == null)
{
GetConnection();
}
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
adapter.SelectCommand = cmd;
PrepareCommand(conn, cmd, cmdType, cmdText, cmdParms);
DataSet ds = new DataSet();
try
{
conn.Open();
adapter.Fill(ds);
conn.Close();
cmd.Parameters.Clear();
return ds;
}
catch (SqlException sqle)
{
throw new Exception(sqle.Message);
}
}
//------------------------------------------------------------------------
public static SqlDataReader ExecuteReader(string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
{
if (conn == null)
{
GetConnection();
}
SqlCommand cmd = new SqlCommand();
PrepareCommand(conn, cmd, cmdType, cmdText, cmdParms);
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader;
}
catch (SqlException sqle)
{
throw new Exception(sqle.Message);
}
}
//------------------------------------------------------------------------
public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
{
if (conn == null)
{
GetConnection();
}
SqlCommand cmd = new SqlCommand();
PrepareCommand(conn, cmd, cmdType, cmdText, cmdParms);
try
{
conn.Open();
int val = cmd.ExecuteNonQuery();
conn.Close();
cmd.Parameters.Clear();
return val;
}
catch (SqlException sqle)
{
throw new Exception(sqle.Message);
}
}
//------------------------------------------------------------------------
public static object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] cmdParms)
{
if (conn == null)
{
GetConnection();
}
SqlCommand cmd = new SqlCommand();
PrepareCommand(conn, cmd, cmdType, cmdText, cmdParms);
try
{
conn.Open();
object val = cmd.ExecuteScalar();
conn.Close();
cmd.Parameters.Clear();
return val;
}
catch (SqlException sqle)
{
throw new Exception(sqle.Message);
}
}
//------------------------------------------------------------------------
private static void PrepareCommand(SqlConnection conn, SqlCommand cmd, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn == null)
{
GetConnection();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
{
cmd.Parameters.Add(parm);
}
}
}
}
}
运行结果及报错内容: 未处理NullReferenceException 未将对象引用设置到对象的实例。