易截截图软件、单文件、免安装、纯绿色、仅160KB

写入ACCESS数据库的问题


date="'"+dtime+","+save[i,0].ToString()+","+save[i,1].ToString()+","+save[i,2].ToString()+","+save[i,3].ToString()+","+save[i,4].ToString()+","+save[i,5].ToString()+","+save[i,6].ToString()+"'";
             
ocmd.CommandText = "insert into hao(日期,NO1,NO2,NO3,NO4,NO5,NO6,NO7) values("+date+")";
ocmd.ExecuteNonQuery();


在上面的代码中,values的值如果用一个字符串变量代替,它就不行。但我又需要这样,请高手给个办法!
没试过……帮顶!

这样写date就应该可以了:
date="'"+dtime+"','"+save[i,0].ToString()+"','"+save[i,1].ToString()+"','"+save[i,2].ToString()+"','"+save[i,3].ToString()+"','"+save[i,4].ToString()+"','"+save[i,5].ToString()+"','"+save[i,6].ToString()+"'";

主要还是拼接字符串的问题
因为有的需要单引号,有的不需要。这里很容易出错。
我个人还是习惯用如下方式
string model="'{0}',{1},{2},{3},{4},{5},{6},{7}";
在使用的时候
string.Format(model,save[i,0],save[i,1],save[i,2],save[i,3],save[i,4],save[i,5],save[i,6],save[i,7]);
这样更清晰。



date="'"+dtime+","+save[i,0].ToString()+","+save[


相关问答:

php 使用 ADODB 连接Access 的问题

<?php

include("adodb/adodb.inc.php");

$db = &NewADOConnection("access");
$access = realpath("Data.mdb");
$dsn = "Provider=Microsoft.J ......

如何通过ADO使用聚合函数来查询(数据库为ACCESS)

_RecordsetPtr CMyAdo::SelectRecord(string &where, string &tablename,string &field, int way )
{
    int ret = 0;
  _RecordsetPtr pset;
CString  sql;
& ......

Vb.net Access查询按年月


    我需要通过单个查询条件按年月,查询到access数据库里的数据为 (日期类型2008-02-02)


        Dim DDTP1 As String

        Dim date1 ......

插入ACCESS数据库出错

C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace DAL
{
public class DBHelp
{
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号