日期:2014-05-18  浏览次数:21094 次

C#.net两个表更新问题
我的数据库里一共有两个表,一个是Pocket_book,一个是Project_book


string MyConn = "server=SQLOLEDB;Data Source=OOCO8HPY5V6TMKN;User ID=sa;Initial Catalog=Licai";
SqlConnection MyConnection = new SqlConnection(MyConn);  
   
  //-----------------select语句--------------------  
SqlCommand MyCommand = new SqlCommand("select Main_ID as 编号,IO_Date as 收支日期,Type as 类型,Project as 项目,Money as 金额,Explain as 说明,Mark as 删除标记 from Pocket_book,Project_book where Pocket_book.A_ID = Project_book.A_ID and Pocket_book.B_ID = Project_book.B_ID", MyConnection);

DataTable dt = new DataTable();
SqlDataAdapter SelectAdapter = new SqlDataAdapter();//定义一个数据适配器
SelectAdapter.SelectCommand = MyCommand;//定义数据适配器的操作指令
DataSet MyDataSet = new DataSet();//定义一个数据集
MyConnection.Open();//打开数据库连接
SelectAdapter.SelectCommand.ExecuteNonQuery();//执行数据库查询指令
SelectAdapter.Fill(MyDataSet, "Pocket_book");
System.Text.StringBuilder strSql = new System.Text.StringBuilder();
dgvInformation.AutoGenerateColumns = true;
dgvInformation.DataSource = MyDataSet.Tables[0];
DataTable dt = MyDataSet.Tables[0];

在datagridview上显示,双击某一行进行修改,将这一行的值传给下一个窗体,在下一个窗体用几个textbox
控件显示这些值,然后进行修改
//-----------------------下面代码是传值--------------
  int ID;
  ID = dgvInformation.CurrentCell.RowIndex;
  MyAccountUpdate frm = new MyAccountUpdate();
  DataTable dt = new DataTable();
  dt = (DataTable)dgvInformation.DataSource;
  frm.txtExplain.Text = dt.Rows[ID]["说明"].ToString();
  frm.txtMain_id.Text = dt.Rows[ID]["编号"].ToString();

  if (Convert.ToInt32(dt.Rows[ID]["类型"]) == 0)//数值类型转化
  {
  frm.txtMoney.Text = dt.Rows[ID]["金额"].ToString();
  frm.cboIncome.Text = dt.Rows[ID]["项目"].ToString();
  }
  if (Convert.ToInt32(dt.Rows[ID]["类型"]) == 1)
  {
  frm.txtMoney.Text = dt.Rows[ID]["金额"].ToString();
  frm.cboOutcome.Text = dt.Rows[ID]["项目"].ToString();
  }
   
  frm.dtpkDate.Text = dt.Rows[ID]["收支日期"].ToString();
  frm.ShowDialog();


值传过来了,也能在这个窗体上显示了,在进行update的时候,就不会了!!!!
怎么写啊
谢谢了!!!
在线等!!!!!!




------解决方案--------------------
sqlcommand去执行update语句
C# code

SqlConnection cn = new SqlConnection("server=*.*.*.*;uid=xxx;pwd=xxx;database=xxxxx");
            SqlCommand cmd = new SqlCommand("update xx set xx = 0 where xx = 1", cn);
            cn.Open();
            MessageBox.Show(cmd.ExecuteNonQuery().ToString());
            cn.Close();