本帖最后由 张彪 于 2011-12-2 13:57 编辑
public static class DBConn
{
public static SqlConnection CyCon()
{
return new SqlConnection (@"Data Source=PC2011102013SPQ\SQLEXPRESS;Initial Catalog=library;Integrated Security=True");
}
// public static SqlConnection conn = new SqlConnection();
}
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = Basebase.DBConn.CyCon();
DateTime date = new DateTime();
try
{
DBConn.CyCon().Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = DBConn.CyCon();
//更新数据
cmd.CommandText = "Insert borrowandback [readerid]='" + textBox1.Text + "'" +
//",[name]='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'" +
",[bookcode]='" + dataGridView1.CurrentRow.Cells[2].Value.ToString() + "'" +
",[borrowTime]='" + date.TimeOfDay + "'" + "'";
//",[paperType]='" + dataGridView1.CurrentRow.Cells[4].Value.ToString() + "'" +
//",[paperNum]='" + dataGridView1.CurrentRow.Cells[5].Value.ToString() + "'" +
//",[tel]='" + dataGridView1.CurrentRow.Cells[6].Value.ToString() + "'" +
//",[email]='" + dataGridView1.CurrentRow.Cells[7].Value.ToString() + "'" +
//",[createDate]='" + dataGridView1.CurrentRow.Cells[8].Value.ToString() + "'" +
//",[oper]='" + dataGridView1.CurrentRow.Cells[9].Value.ToString() + "'" + "where[id]='" + "'";
cmd.ExecuteNonQuery();
//刷新显示结果
DataSet dalibrary = new DataSet();
SqlDataAdapter dabaseInfrom = new SqlDataAdapter("Select* from borrowandback", DBConn.CyCon());
dabaseInfrom.Fill(dalibrary, "borrowandback");
//绑定数据库
dataGridView1.DataSource = dalibrary.Tables["borrowandback"];
MessageBox.Show("修改成功");
DBConn.CyCon().Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "打开数据库失败!");
}
}
数据库连接正确为什么总是出现
|