0
我試圖插入數據庫的一些線列,但在執行這個代碼,我得到了一個錯誤:C++ ADO.NET無法插入NULL值插入
#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值插入 列'fixee',表'MonitoringN.dbo.Alerte'; 列不允許有空值。
有什麼好主意嗎?
這是正確的,我有一個ID列先自動生成的Alerte的表中,所以我不能插入它,這就是爲什麼我'沒有初始化它的價值。然後,我更新了我的代碼'myDataAdapter-> SelectCommand = gcnew SqlCommand(「select motif,dateAlerte,fixee,nomPoste」+「nomApplication,nomFichier,FichierModel_id from Alerte」,mySQLConnection);我也有同樣的錯誤。' – user3264174
嗯......你試過把名字的值分配給你的myRow嗎?在C#中它應該像這樣工作:myRow [「fixee」] = true; ...並且在將新行插入調試器後,您是否已查看過表格和表格? – PrfctByDsgn
當你刪除這樣的行時它是否工作:myParameter-> SourceVersion = DataRowVersion :: Current; ?我不認爲這有用... – PrfctByDsgn