SQL~~汇总

 //合并数据表

用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);
             }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值