access检测表没有的字段,添加之

   #region 检测是否存在STATE字段,没有就添加
        /// <summary>
        /// 检测是否存在STATE字段,没有就添加
        /// </summary>
        /// <returns></returns>
        public static bool CheckAndAddColumn()
        {
            try
            {
                using (OleDbConnection conn = new OleDbConnection(Provider=Microsoft.Jet.OleDb.4.0;Data Source=数据库的完整版路径(.mdb文件);))
                {
                    conn.Open();
                    string strStateSql = "select * from T";
                    OleDbCommand stateCmd = new OleDbCommand(strStateSql, conn);
                    DataSet dt = new DataSet();
                    OleDbDataAdapter da = new OleDbDataAdapter(stateCmd);
                    da.Fill(dt);

                    if (dt.Tables[0].Columns["STATE"] == null)
                    {
                        string addSateSql = "ALTER TABLE T ADD COLUMN STATE Short";
                        OleDbCommand cmd = new OleDbCommand(addSateSql, conn);
                        if (cmd.ExecuteNonQuery() >= 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        } 
        #endregion

 

posted @ 2015-12-14 16:56  洛晨随风  阅读(436)  评论(0编辑  收藏  举报