private bool saveDATA() { Boolean tempvalue = false; //定义返回值,并设置初值 //下面把note中的数据添加到数据库中! try { System.Data.DataSet mydataset; //定义DataSet OleDbConnection conn = SaveDate.getConn(); //getConn():得到连接对象 OleDbDataAdapter adapter = new OleDbDataAdapter(); string sqlstr = "select * from mdb where Photo='" + class1.photo + "' and TextName='" + class1.TextName + "'"; mydataset = new System.Data.DataSet(); adapter.SelectCommand = new OleDbCommand(sqlstr, conn); adapter.Fill(mydataset, "notes"); conn.Close(); if (mydataset.Tables[0].Rows.Count > 0) { return(tempvalue); } else { #region MyRegion OleDbConnection conn_ = SaveDate.getConn(); conn_.Open(); string qk = class1.ID == "1" ? "58同城" : "赶集网"; //设置SQL语句 string insertstr = "INSERT INTO mdb(Counts, Photo, Personname, Times, Href,del, TextName ,http,Addess,PM,Moneys,datetimes) VALUES ('"; insertstr += 0 + "', '"; insertstr += class1.photo + "','"; insertstr += class1.PersonName + "','"; insertstr += DateTime.Now.ToString("yyyy-MM-dd") + "','"; insertstr += class1.href + "','"; insertstr += 0 + "','"; insertstr += class1.TextName + "','"; insertstr += qk + "','"; insertstr += class1.Address + "','"; insertstr += class1.Allpm + "','"; insertstr += class1.SumMoney + "','"; insertstr += class1.datetime + "')"; OleDbCommand insertcmd = new OleDbCommand(insertstr, conn_); insertcmd.ExecuteNonQuery(); conn_.Close(); tempvalue = true; #endregion } } catch (Exception e) { throw (new Exception("数据库出错:" + e.Message)); } return(tempvalue); }
private void buttons_Click(object sender, RoutedEventArgs e) { #region 更新表单 Boolean tempvalue = false; string sqlstr = ""; //当时在这里定义,是为了在出现异常的时候看看我的SQL语句是否正确 try { //用到了我前面写的那个得到数据库连接的函数 OleDbConnection conn = SaveDate.getConn(); //getConn():得到连接对象, conn.Open(); //确定我们需要执行的SQL语句,本处是UPDATE语句! sqlstr = "UPDATE Author SET "; sqlstr += "Author='" + zztext.Text.Trim() + "',"; sqlstr += "Photo='" + phototext.Text.Trim() + "',"; sqlstr += "QQ='" + qqtext.Text.Trim() + "'"; sqlstr += " where id=4"; //定义command对象,并执行相应的SQL语句 OleDbCommand myCommand = new OleDbCommand(sqlstr, conn); myCommand.ExecuteNonQuery(); //执行SELECT的时候我们是用的ExecuteReader() conn.Close(); //假如执行成功,则,返回TRUE,否则,返回FALSE tempvalue = true; } catch (Exception es) { MessageBox.Show("数据库更新出错:" + sqlstr + "\r" + es.Message); } #endregion if (tempvalue) { zztext.IsReadOnly = true; phototext.IsReadOnly = true; qqtext.IsReadOnly = true; zztext.BorderThickness = new Thickness(0); phototext.BorderThickness = new Thickness(0); qqtext.BorderThickness = new Thickness(0); buttons.Visibility = Visibility.Collapsed; } }
private void Window_Loaded(object sender, RoutedEventArgs e) { DataView dataview; System.Data.DataSet mydataset; //定义DataSet try { OleDbConnection conn = SaveDate.getConn(); //getConn():得到连接对象 OleDbDataAdapter adapter = new OleDbDataAdapter(); string sqlstr = "select * from Author where ID=4"; mydataset = new System.Data.DataSet(); adapter.SelectCommand = new OleDbCommand(sqlstr, conn); adapter.Fill(mydataset, "notes"); conn.Close(); } catch (Exception es) { throw (new Exception("数据库出错:" + es.Message)); } dataview = new DataView(mydataset.Tables["notes"]); zztext.Text = dataview.Table.Rows[0]["Author"].ToString(); phototext.Text = dataview.Table.Rows[0]["Photo"].ToString(); qqtext.Text = dataview.Table.Rows[0]["QQ"].ToString(); }