ConnectionString 属性尚未初始化
public partial class User : Form
{
public User()
{
InitializeComponent();
}
public DataSet ds = new DataSet();
SqlDataAdapter sda1 = null;
string username;
string userpsw;
string userlevel;
//编辑现有用户
private void BT_edit_Click(object sender, EventArgs e)
{
sda1.Update(ds, "a");
Databind();
}
//删除所选用户
private void BT_delete_Click(object sender, EventArgs e)
{
}
//重载控件
private void BT_refresh_Click(object sender, EventArgs e)
{
// 刷新datagridview 把当前的form重新加载
User_Load(sender, e);
}
//数据绑定方法
public void Databind()
{
SqlConnection scon1 = new SqlConnection("Data Source=acer-PC;Initial Catalog=cmis;Integrated Security=True;Pooling=False");
string sqlconnet1 = "select [username] '名称',[userpsw] '密码',[userlevel] '等级' from [users]";
SqlCommand scmd1 = new SqlCommand(sqlconnet1, scon1);
sda1 = new SqlDataAdapter(scmd1);
sda1.SelectCommand = scmd1;
SqlCommandBuilder scb = new SqlCommandBuilder(sda1);
sda1.InsertCommand = scb.GetInsertCommand();
sda1.DeleteCommand = scb.GetDeleteCommand();
sda1.UpdateCommand = scb.GetUpdateCommand();
try
{
scon1.Open();
ds.Clear();
sda1.Fill(ds, "a");
this.DATA_user.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show("数据库异常");
}
finally
{
scon1.Close();
scon1.Dispose();
scmd1.Dispose();
sda1.Dispose();
}
}
private void User_Load(object sender, EventArgs e)
{
Databind();
}
------解决方案--------------------
C# code
SqlCommand scmd1 = new SqlCommand(sqlconnet1);
//下面行添加 ,scon1
sda1 = new SqlDataAdapter(scmd1,scon1);
sda1.SelectCommand = scmd1;
SqlCommandBuilder scb = new SqlCommandBuilder(sda1);
------解决方案--------------------
ConnectionString配置不正确。