private void btnSaveToDB_Click(object sender, EventArgs e) { StringBuilder sbTDH = new StringBuilder(); StringBuilder sbJLZ = new StringBuilder(); StringBuilder sbSK = new StringBuilder(); StringBuilder sbSKC = new StringBuilder(); StringBuilder sbCKZ = new StringBuilder(); StringBuilder sbYSSJ = new StringBuilder(); StringBuilder sbDT = new StringBuilder(); for (int i = 0; i < dgvData.Rows.Count - 1; i++) { if (dgvData.Rows[i].Cells["ColDataType"].Value.ToString() != "线圈") { //for (int j = 1; j < dgvData.Columns.Count; j++) //{ sbTDH.Append(dgvData.Rows[i].Cells["ColTDH"].Value + ","); sbJLZ.Append(dgvData.Rows[i].Cells["ColJLZ"].Value + ","); sbSK.Append(dgvData.Rows[i].Cells["ColSK"].Value + ","); sbSKC.Append(dgvData.Rows[i].Cells["ColSKC"].Value + ","); sbCKZ.Append(dgvData.Rows[i].Cells["ColCKZ"].Value + ","); sbDT.Append(dgvData.Rows[i].Cells["ColRecvTime"].Value + ","); sbYSSJ.Append(dgvData.Rows[i].Cells["ColRecvCode"].Value + ","); // } } } DataBase.DALCBData dalCBData = new DataBase.DALCBData(); DataBase.CBDataTable tabCBData = new DataBase.CBDataTable(); tabCBData.TDH = sbTDH.ToString(); tabCBData.JLZ = sbJLZ.ToString(); tabCBData.SK = sbSK.ToString(); tabCBData.SKC = sbSKC.ToString(); tabCBData.CKZ = sbCKZ.ToString(); tabCBData.JSSJ = sbDT.ToString(); tabCBData.YSSJ = sbYSSJ.ToString(); if (dalCBData.Add(tabCBData) > 0) { MessageBox.Show("保存成功"); dgvData.Rows.Clear(); } else { MessageBox.Show("保存失败"); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(CBDataTable model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update CBData set "); strSql.Append("TDH=@TDH,"); strSql.Append("JLZ=@JLZ,"); strSql.Append("SK=@SK,"); strSql.Append("SKC=@SKC,"); strSql.Append("CKZ=@CKZ,"); strSql.Append("JSSJ=@JSSJ,"); strSql.Append("YSSJ=@YSSJ"); strSql.Append(" where ID=@ID"); SqlParameter[] parameters = { new SqlParameter("@TDH", SqlDbType.NVarChar, 100), new SqlParameter("@JLZ", SqlDbType.NVarChar, 500), new SqlParameter("@SK", SqlDbType.NVarChar, 500), new SqlParameter("@SKC", SqlDbType.NVarChar, 500), new SqlParameter("@CKZ", SqlDbType.NVarChar, 500), new SqlParameter("@JSSJ", SqlDbType.NVarChar, 1000), new SqlParameter("@YSSJ", SqlDbType.NVarChar, 4000), new SqlParameter("@ID", SqlDbType.Int, 4) }; parameters[0].Value = model.TDH; parameters[1].Value = model.JLZ; parameters[2].Value = model.SK; parameters[3].Value = model.SKC; parameters[4].Value = model.CKZ; parameters[5].Value = model.JSSJ; parameters[6].Value = model.YSSJ; parameters[7].Value = model.ID; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 得到一个对象实体 /// </summary> public CBDataTable DataRowToModel(DataRow row) { CBDataTable model = new CBDataTable(); if (row != null) { if (row["ID"] != null && row["ID"].ToString() != "") { model.ID = int.Parse(row["ID"].ToString()); } if (row["TDH"] != null) { model.TDH = row["TDH"].ToString(); } if (row["JLZ"] != null) { model.JLZ = row["JLZ"].ToString(); } if (row["SK"] != null) { model.SK = row["SK"].ToString(); } if (row["SKC"] != null) { model.SKC = row["SKC"].ToString(); } if (row["CKZ"] != null) { model.CKZ = row["CKZ"].ToString(); } if (row["JSSJ"] != null) { model.JSSJ = row["JSSJ"].ToString(); } if (row["YSSJ"] != null) { model.YSSJ = row["YSSJ"].ToString(); } } return(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(CBDataTable model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into CBData("); strSql.Append("TDH,JLZ,SK,SKC,CKZ,JSSJ,YSSJ)"); strSql.Append(" values ("); strSql.Append("@TDH,@JLZ,@SK,@SKC,@CKZ,@JSSJ,@YSSJ)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@TDH", SqlDbType.NVarChar, 100), new SqlParameter("@JLZ", SqlDbType.NVarChar, 500), new SqlParameter("@SK", SqlDbType.NVarChar, 500), new SqlParameter("@SKC", SqlDbType.NVarChar, 500), new SqlParameter("@CKZ", SqlDbType.NVarChar, 500), new SqlParameter("@JSSJ", SqlDbType.NVarChar, 1000), new SqlParameter("@YSSJ", SqlDbType.NVarChar, 4000) }; parameters[0].Value = model.TDH; parameters[1].Value = model.JLZ; parameters[2].Value = model.SK; parameters[3].Value = model.SKC; parameters[4].Value = model.CKZ; parameters[5].Value = model.JSSJ; parameters[6].Value = model.YSSJ; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 得到一个对象实体 /// </summary> public CBDataTable GetModel(int ID) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 ID,TDH,JLZ,SK,SKC,CKZ,JSSJ,YSSJ from CBData "); strSql.Append(" where ID=@ID"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) }; parameters[0].Value = ID; CBDataTable model = new CBDataTable(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }