C ++ ADO.NET无法将值NULL插入列中

时间:2014-02-05 23:06:15

标签: c++ sql-server-2008 ado.net

我正在尝试在数据库中插入一些行,但在执行此代码时出错:

#using <mscorlib.dll>
#using <System.dll>
#using <system.data.dll>
#using <System.Xml.dll>

#include <sstream>
#include <time.h>
#include <string>
#include <iostream>
#include <tchar.h>

using namespace System;
using namespace System::Data;
using namespace System::Xml;
using namespace System::Data::SqlClient;

// This is the entry point for this application
int _tmain(void)
{
    SqlConnection      ^ mySQLConnection;
    SqlDataAdapter     ^ myDataAdapter;
    DataSet            ^ myDataSet;
    DataRow            ^ myRow;
    SqlParameter       ^ myParameter;

    try
    {
        mySQLConnection = gcnew SqlConnection("Data Source=NECTARYS-PC;Initial Catalog=MonitoringN;Integrated Security=True;");
        myDataAdapter = gcnew SqlDataAdapter();
        myDataSet = gcnew DataSet();

        // Open up the connection
        mySQLConnection->Open();

        myDataAdapter->SelectCommand = gcnew SqlCommand("select motif,dateAlerte,fixee,nomPoste," +
        "nomApplication,nomFichier,FichierModel_id from Alerte", mySQLConnection);

        myDataAdapter->InsertCommand = gcnew SqlCommand("insert into Alerte (motif,dateAlerte," +
            "fixee,nomPoste,nomApplication,nomFichier,FichierModel_id) values (@motif,@dateAlerte," +
            "@fixee,@nomPoste,@nomApplication,@nomFichier,@FichierModel_id)", mySQLConnection);

        myParameter = myDataAdapter->InsertCommand->Parameters->Add(
            gcnew SqlParameter("@motif", SqlDbType::VarChar));
        myParameter->SourceColumn = "motif";
        myParameter->SourceVersion = DataRowVersion::Current;

        myParameter = myDataAdapter->InsertCommand->Parameters->Add(
            gcnew SqlParameter("@dateAlerte", SqlDbType::VarChar));
        myParameter->SourceColumn = "dateAlerte";
        myParameter->SourceVersion = DataRowVersion::Current;

        myParameter = myDataAdapter->InsertCommand->Parameters->Add(
            gcnew SqlParameter("@fixee", SqlDbType::Bit));
        myParameter->SourceColumn = "fixee";
        myParameter->SourceVersion = DataRowVersion::Current;

        myParameter = myDataAdapter->InsertCommand->Parameters->Add(
            gcnew SqlParameter("@nomPoste", SqlDbType::VarChar));
        myParameter->SourceColumn = "nomPoste";
        myParameter->SourceVersion = DataRowVersion::Current;

        myParameter = myDataAdapter->InsertCommand->Parameters->Add(
            gcnew SqlParameter("@nomApplication", SqlDbType::VarChar));
        myParameter->SourceColumn = "nomApplication";
        myParameter->SourceVersion = DataRowVersion::Current;

        myParameter = myDataAdapter->InsertCommand->Parameters->Add(
            gcnew SqlParameter("@nomFichier", SqlDbType::VarChar));
        myParameter->SourceColumn = "nomFichier";
        myParameter->SourceVersion = DataRowVersion::Current;

        myParameter = myDataAdapter->InsertCommand->Parameters->Add(
            gcnew SqlParameter("@FichierModel_id", SqlDbType::Int));
        myParameter->SourceColumn = "FichierModel_id";
        myParameter->SourceVersion = DataRowVersion::Current;

        time_t now = time(0);
        tm ltm;
        localtime_s(&ltm,&now);
        std::stringstream dateAjoutSysteme;
        dateAjoutSysteme << ltm.tm_mday
            << "/"
            << 1 + ltm.tm_mon
            << "/"
            << 1900 + ltm.tm_year
            << " "
            << 1 + ltm.tm_hour
            << ":"
            << 1 + ltm.tm_min
            << ":"
            << 1 + ltm.tm_sec;
        String^ str = gcnew System::String(dateAjoutSysteme.str().c_str());

        Boolean fixee = true;

        myDataAdapter->Fill(myDataSet, "Alerte");
        myRow = myDataSet->Tables["Alerte"]->NewRow();
        myRow->ItemArray[0] = "A";
        myRow->ItemArray[1] = str;
        myRow->ItemArray[2] = fixee;
        myRow->ItemArray[3] = "B";
        myRow->ItemArray[4] = "C";
        myRow->ItemArray[5] = "D";
        myRow->ItemArray[6] = 0UL;
        myDataSet->Tables["Alerte"]->Rows->Add(myRow);

        //we use insertcommand property for the update.
        myDataAdapter->Update(myDataSet, "Alerte");
    }
    catch (Exception ^ e) {
        Console::Write(e->ToString());
    }
    __finally {
        mySQLConnection->Close();
        system("PAUSE");
    }
    return 0;
}

错误:

  

System.Data.SqlClient.SqlException(0x80131904):无法插入   值'NULL'进入'fixee'列,表'MonitoringN.dbo.Alerte';   列不允许空值。

有什么好主意吗?

1 个答案:

答案 0 :(得分:0)

你在Alerte上做了一个选择*所以你不知道列的顺序...然后你根据列索引分配值...我猜有问题... fixee可能是不是myDataSet中的第二列 - &gt; Tables [“Alerte”]