![](/img/trans.png)
[英]How to set a NULL column value through an ADO query connected to a MySQL ODBC source in C++?
[英]C++ ADO.NET Cannot insert the value NULL into column
我正在嘗試在數據庫中插入一些行,但是執行此代碼時出現錯誤:
#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(<m,&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插入表'MonitoringN.dbo.Alerte'的列'fixee'中; 列不允許為空。
有什么好主意嗎?
您在Alerte上進行了選擇* ...因此您不知道列的順序...然后由於列索引而分配了值...我想這是個問題...固定對象可能不是第二個myDataSet-> Tables [“ Alerte”]中的列
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.