日期:2014-05-17  浏览次数:21155 次

c# 链接数据库报错
给报的错误是这个Command text was not set for the command object。
还有过no file name 之类的错,莫名的这个错就没了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
  class accessData
  {
  #region Access 2010
  hospital hospitalData;
  public accessData(hospital h)
  {
  hospitalData = h;
  }
   
  public void runData() 
  {
  string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
  strConnection += @"Data Source=C:\Users\lisca\Desktop\patient.accdb;";
   
  OleDbConnection vcon = new OleDbConnection(strConnection);
  try
  {
  vcon.Open();
  // string sqlCommand = @"INSERT INTO patientData(patientID,visitID) VALUES(script,qty5')";
  System.Console.WriteLine("open");
  OleDbCommand mycmd = new OleDbCommand("", vcon);

  int currentPid = 0, currentVID = 0;
  string sqlCommand = "";
  for (int i = 0; i < hospitalData.getPatientList().Count; i++)
  {
  currentPid = i;

  for (int j = 0; j < hospitalData.getPatient(i).getVisitInfo().Count; j++)
  {
  currentVID = j;
  sqlCommand = @"INSERT INTO patientData(patientID,visitID) VALUES('" + currentPid + "," + currentVID + ')';

  mycmd = new OleDbCommand(sqlCommand, vcon);
  }

  }

  }
  catch (Exception e)
  {

  Console.WriteLine("what on earth happened!?", e.Message);

  } 



  mycmd.ExecuteNonQuery();
   
   

  vcon.Close();

  }
  #endregion
  }

}


还有,虽然我C:\Users\lisca\Desktop\patient.accdb路径确实正确,但是我不知道能不能写进去。

insert的变量是两个,patientID 和 visitID。会不会是这里不对。

加上try 和catch之后,就只输出writeline的string,e.message没有内容。
急等,万分感谢



------解决方案--------------------
script,qty5这两个是什么东西,对id字段赋值,应该是数值吧,如果这两个是变量的话,也不能这样直接写啊
string sqlCommand = @"INSERT INTO patientData(patientID,visitID) VALUES("+script.ToString()+","+qty5.ToString()+")";
------解决方案--------------------
调试一下 vcon.Open();这句有没有报错,有报错的话,连接都没有连接上 

sqlCommand = @"INSERT INTO patientData(patientID,visitID) VALUES('" + currentPid + "," + currentVID + ')';
如果这句报错的话:检查patientData这个数据库表里面是否有这两个字段、和他们的类型,如果是int型的,VALUES(currentPid,currentVID)';是varchar的话,VALUES('" + currentPid + "','" + currentVID + ')';
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
class accessData
{
#region Access 2010