using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace chaohsi
{
public partial class LoginForm : Form
{
//定义连接字符串变量,从配置文件中读取连接字符串
private string conString = ConfigurationManager.ConnectionStrings["chaoshi"].ConnectionString.ToString();
public LoginForm()
{
InitializeComponent();
}
///
/// 登录处理程序
/////////private void button1_Click(object sender, EventArgs e){//(1)获取输入数据string userid = this.txtUserID.Text.Trim();string pwd = this.txtPwd.Text.Trim();//(2)建立数据库连接对象//连接字符串string conString = @"Data Source=LAPTOP-0KJBJLJG\MSSQLSERVER01;Initial Catalog=db_chaoshi;Integrated Security=true";//构造SQL命令string strSQL = string.Format("select * from [tb_User] where UserID='{0}' and UserPwd='{1}'", userid, pwd);using (SqlConnection con = new SqlConnection(conString)){//建立命令对象SqlCommand cmd = new SqlCommand(strSQL, con);//打开数据库连接con.Open();//执行命令,返回结果集//object obj = cmd.ExecuteScalar();SqlDataReader sdr = cmd.ExecuteReader();//根据结果处理if (sdr.HasRows == false) //没有指定用户{MessageBox.Show("用户名或密码错误!", "用户登录");this.txtUserID.Clear();this.txtPwd.Clear();this.txtUserID.Focus();}else{MessageBox.Show("登录成功!", "用户登录");//读取用户名,保存sdr.Read();//将用户姓名保存Users.userName = Convert.ToString(sdr["UserName"]);//记录登录窗体的结果this.DialogResult = DialogResult.OK;}}}}class Users{public static string userName { set; get; }}}
private string conString = ConfigurationManager.ConnectionStrings["chaoshi"].ConnectionString.ToString();这句话说引发异常,System.NullReferenceException:末将对象引用设置到对象实例
你的chaoshi 要么写错了 要么就空了