新增记录/// <summary> /// 新增记录 /// </summary> /// <param name="spath">数据库全名</param> /// <param name="dataname">表名</param> /// <param name="captions">字段名</param> /// <param name="items">添加的纪录内容</param> public void newdata(string spath, string dataname, string[] captions, object[] items) { try { //连接到一个数据库 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + spath; OleDbConnection myConn = new OleDbConnection(strCon); myConn.Open(); string strInsert; int tt = captions.Length; int sign = -1;//记录日期字段所在索引号,用来格式化日期格式(只要日期,不要时间)
strInsert = " INSERT INTO " + dataname + " ( " + captions[0] + " , ";
for (int i = 1; i < tt - 1; i++) { if (captions[i].Contains("日期")) { sign = i; } strInsert += captions[i] + " , "; } strInsert += captions[tt - 1] + " ) VALUES ( ' ";
for (int i = 0; i < tt - 1; i++) { if (i == sign) { string[] ss = items[i].ToString().Split(' '); strInsert += ss[0] + " ' , ' "; } else { strInsert += items[i].ToString() + " ' , ' "; } } strInsert += items[tt - 1].ToString() + " ') "; OleDbCommand myCommand = new OleDbCommand(strInsert, myConn); myCommand.ExecuteNonQuery(); myConn.Close(); } catch (Exception ed) { MessageBox.Show("新增记录错误信息: " + ed.ToString(), "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
like和not like适合字符型字段的查询,%%代表任意长度的字符串,_下划线代表一个任意的字符。like ‘m%%’ 代表m开头的任意长度的字符串,like ‘m__’ 代表m开头的长度为3的字符串。ADO中的通配符是两个"%%",还有上面的关键自都要大写,条件中的值大多要有用单引号括起来.