//合并数据表
用DataTable类的Merge()
tableA->Merge(tableB);
//异步方式访问数据库
String^ SQLSTR = SQL查询语句;
String^ ConnectionStr = SQL数据库连接命令;
SqlConnection^ MyConnection = gcnew SqlConnection(ConnectionStr);
SqlCommand^ MyCommand = gcnew SqlCommand(SQLSTR, MyConnection);
MyConnection->Open();
IAsyncResult^ MyResult = MyCommand->BeginExecuteReader();
while(!MyResult->IsCompleted){
System::Threading::Thread::Sleep(10);
}
SqlDataReader^ MyReader = MyCommand->EndExecuteReader(MyResult);
DataTable^ MyTable = gcnew DataTable();
MyTable->Load(MyReader);
this->dataGridView1->DataSource = MyTable;
MyConnection->Close();
//从其它数据源导入数据 数据源类型不限 目标数据库限定为SQL
SqlConnection^ msc = new SqlConnection(".........");
msc->Open();
SqlBulkCopy^ sqlbc = gcnew SqlBulkCopy(msc);
sqlbc->DestinationTableName = 目标表名;
sqlbc->WriteToServer((DataTable^)this->dataGridView1->DataSource);
msc->Close();
MessageBox::Show("导入操作结束", "", MessageBoxButtons::OK, MessageBoxIcon::Information);
//SqlHelper操作
using namespace Microsoft::ApplicationBlocks::Data;
//打开db
this->dataSet1 = SqlHelper::ExecuteDataset("(数据库连接),CommandType::Text, String^ SQLstr");
this->dataGridView->DataSource = this->dataSet1->Tables[0];
//增加记录
array<System::Data::SqlClient::SqlParameter^> Parameter = gcnew array<System::Data::SqlClient::SqlParameter^>(*);
Parameter[] = gcnew System::Data::SqlClient::SqlParameter("@",SqlDbType::数据类型);
Parameter[] = String^ str;
SqlHelper::ExecuteScalar("(数据库连接),CommandType::Text,(SQL语句) ,Parameter")
//统计记录数(获取sql语句聚合函数值)
str = "select count(*) from *"
SqlHelper::ExecuteDataset("(数据库连接),CommandType::Text, str")后返回记录数
//sqlcommand参数添加记录
this->sqlCommand1 = gcnew SqlCommand("insert into One values(@ID,@姓名,@性别)", this->sqlConnection1);
this->sqlCommand1->Parameters->Add(gcnew SqlParameter("@ID", SqlDbType::BigInt));
this->sqlCommand1->Parameters->Add(gcnew SqlParameter("@姓名", SqlDbType::NChar,10));
this->sqlCommand1->Parameters->Add(gcnew SqlParameter("@性别", SqlDbType::NChar,10));
this->sqlCommand1->Prepare();
this->sqlCommand1->Parameters["@ID"]->Value = this->textBox1->Text;
this->sqlCommand1->Parameters["@姓名"]->Value = this->textBox1->Text;
this->sqlCommand1->Parameters["@性别"]->Value = this->textBox1->Text;
this->sqlCommand1->ExecuteNonQuery();
//SQL数据库添加一行数据
try{
array<DataColumn^>^ Myrecord = gcnew array<DataColumn^>(列数);
Myrecord[0] = mysql2005DataSet->Tables["表名"]->Columns["列名"];
Myrecord[1] = mysql2005DataSet->Tables["……"]->Columns["……"];
mysql2005DataSet->Tables["表名"]->PrimaryKey = Myrecord;
array<System::String^>^ MyRow = {this->textBox1->Text, ……};
DataRow^ MM = mysql2005DataSet->Tables["Character"]->Rows->Find(MyRow);
if(MM == nullptr){
DataRow^ MyNewRow = mysql2005DataSet->Tables["表名"]->NewRow();
MyNewRow["列名"] = MySupplier[0];
MyNewRow["……"] = MySupplier[1];
mysql2005DataSet->Tables["表名"]->Rows->Add(MyNewRow);
}
else{
MessageBox::Show("记录已经存在", "", MessageBoxButtons::OK, MessageBoxIcon::Information);
return;
}
CharacterTableAdapter->Update(this->mysql2005DataSet);
MessageBox::Show("success", "", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
catch(Exception^ eee){
MessageBox::Show(eee->Message, "", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
}
//通过Connetion读取数据
try{
this->richTextBox1->Text = "";
this->sqlConnection1->ConnectionString = L"Data Source=CZ;Initial Catalog=mysql2005;Integrated Security =SSPI; User ID=sa";
this->sqlConnection1->Open();
this->sqlCommand1->Connection = this->sqlConnection1;
this->sqlCommand1->CommandText = this->textBox12->Text;
System::Data::SqlClient::SqlDataReader^ MyReader = this->sqlCommand1->ExecuteReader();
while(MyReader->Read()){
for(int i = 0; i < MyReader->FieldCount; i++){
this->richTextBox1->Text += MyReader[i]->ToString() + "/t";
}
this->richTextBox1->Text += "/n";
}
MyReader->Close();
sqlConnection1->Close();
}
catch(Exception^ eee){
MessageBox::Show(eee->Message, "", MessageBoxButtons::OK, MessageBoxIcon::Information);
}