//读取NewBookInfo数据表记录
try
{
string MySQL = "Select * From NewBookInfo ";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.;Initial Catalog=Library;Integrated Security=True;Pooling=False;Asynchronous Processing=true";
conn.Open();
SqlCommand sqlCommand = new SqlCommand(MySQL, conn);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds, "NewBookInfo"); dataGridView4.DataSource = ds.Tables[0].DefaultView;
//更新数据 int MyCount = sqlDataAdapter.Update(ds, "NewBookInfo");
string MyInfo = "成功更新 " + MyCount.ToString() + " 条记录!";
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} 为什么不能更新数据到数据库,只是提示"成功更新0条记录!"
try
{
string MySQL = "Select * From NewBookInfo ";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.;Initial Catalog=Library;Integrated Security=True;Pooling=False;Asynchronous Processing=true";
conn.Open();
SqlCommand sqlCommand = new SqlCommand(MySQL, conn);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds, "NewBookInfo"); dataGridView4.DataSource = ds.Tables[0].DefaultView;
//更新数据 int MyCount = sqlDataAdapter.Update(ds, "NewBookInfo");
string MyInfo = "成功更新 " + MyCount.ToString() + " 条记录!";
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} 为什么不能更新数据到数据库,只是提示"成功更新0条记录!"
你没有对数库的更新操作,怎么更新数据?
public SqlConnection conn;
private SqlCommand cmd;
private SqlDataAdapter da;
private SqlCommandBuilder scb;
private DataSet ds;
private DataRow row;
private DataTable dt;
private FileStream file;
private String connstring = @"Data Source=AA-7167277DCD6B;Initial Catalog=mydata;Integrated Security=True";
public SQL()
{
conn = new SqlConnection();
conn.ConnectionString = connstring;
} public bool update0(string x1, string x2)
{
try
{
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = x1;
cmd.Parameters.Add(new SqlParameter("@id", x2));
da = new SqlDataAdapter(cmd);
ds = new DataSet("myseat");
scb = new SqlCommandBuilder(da);
da.Fill(ds);
dt = ds.Tables[0];
dt.TableName = "seat";
dt.PrimaryKey = new DataColumn[] { dt.Columns["SeatId"] };
row = ds.Tables[0].Rows[0];
row["State"] = 0;
da.Update(ds, "seat"); conn.Close();
return true;
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
return false;
}
}
自己去看吧