private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("提示:用户名不能为空!");
}
else
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=User;Integrated Security=True");
conn.Open();
SqlDataAdapter allTableAdapter = new SqlDataAdapter("select* from total where ID LIKE '" + textBox1.Text.Trim() + "%'",conn);
DataSet ds = new DataSet();
allTableAdapter.Fill(ds,"tota");
//将获取的数据填充到数据集ds中
//数据源绑定到dataGridView
DataTable dt = ds.Tables["tota"];
dataGridView1.DataSource = dt.DefaultView;
conn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=User;Integrated Security=True");
conn.Open();
DataTable changeDt = dt.GetChanges();
foreach (DataRow dr in changeDt.Rows)
{
string strSQL = string.Empty;
if (dr.RowState == System.Data.DataRowState.Added)
{
strSQL = @"INSERT INTO [User].[dbo].[total]
([ID]
,[PW]
,[type]
,[login])
VALUES
('" + dr["ID"].ToString() + @"'
,'" + dr["PW"].ToString() + @"'
,'" + Convert.ToInt32(dr["type"]) + @"'
,'" + dr["login"].ToString() + @"')";
SqlCommand comm = new SqlCommand(strSQL, conn);
comm.ExecuteNonQuery();
}
}
}
上面是代码,但会一直显示错误说dt不存在,这是为什么呀