⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.h

📁 vc++ 6.0用ado操作sql server
💻 H
📖 第 1 页 / 共 2 页
字号:
			// 
			this->tabPage3->Controls->Add(this->dataGridView3);
			this->tabPage3->Location = System::Drawing::Point(4, 21);
			this->tabPage3->Name = L"tabPage3";
			this->tabPage3->Size = System::Drawing::Size(574, 394);
			this->tabPage3->TabIndex = 2;
			this->tabPage3->Text = L"关系";
			this->tabPage3->UseVisualStyleBackColor = true;
			// 
			// dataGridView3
			// 
			this->dataGridView3->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
			this->dataGridView3->Dock = System::Windows::Forms::DockStyle::Fill;
			this->dataGridView3->Location = System::Drawing::Point(0, 0);
			this->dataGridView3->Name = L"dataGridView3";
			this->dataGridView3->RowTemplate->Height = 23;
			this->dataGridView3->Size = System::Drawing::Size(574, 394);
			this->dataGridView3->TabIndex = 0;
			// 
			// tabPage4
			// 
			this->tabPage4->Controls->Add(this->dataGridView4);
			this->tabPage4->Location = System::Drawing::Point(4, 21);
			this->tabPage4->Name = L"tabPage4";
			this->tabPage4->Size = System::Drawing::Size(574, 394);
			this->tabPage4->TabIndex = 5;
			this->tabPage4->Text = L"查询";
			this->tabPage4->UseVisualStyleBackColor = true;
			// 
			// dataGridView4
			// 
			this->dataGridView4->AllowUserToAddRows = false;
			this->dataGridView4->AllowUserToDeleteRows = false;
			this->dataGridView4->ClipboardCopyMode = System::Windows::Forms::DataGridViewClipboardCopyMode::EnableAlwaysIncludeHeaderText;
			this->dataGridView4->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
			this->dataGridView4->Dock = System::Windows::Forms::DockStyle::Fill;
			this->dataGridView4->Location = System::Drawing::Point(0, 0);
			this->dataGridView4->Name = L"dataGridView4";
			this->dataGridView4->ReadOnly = true;
			this->dataGridView4->RowTemplate->Height = 23;
			this->dataGridView4->Size = System::Drawing::Size(574, 394);
			this->dataGridView4->TabIndex = 0;
			// 
			// textBox1
			// 
			this->textBox1->AcceptsReturn = true;
			this->textBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left) 
				| System::Windows::Forms::AnchorStyles::Right));
			this->textBox1->Location = System::Drawing::Point(4, 425);
			this->textBox1->Multiline = true;
			this->textBox1->Name = L"textBox1";
			this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Both;
			this->textBox1->Size = System::Drawing::Size(509, 74);
			this->textBox1->TabIndex = 2;
			// 
			// button3
			// 
			this->button3->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
			this->button3->Location = System::Drawing::Point(519, 452);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(63, 21);
			this->button3->TabIndex = 3;
			this->button3->Text = L"提交修改";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(584, 502);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->tabControl1);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->tabControl1->ResumeLayout(false);
			this->tabPage1->ResumeLayout(false);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->EndInit();
			this->tabPage2->ResumeLayout(false);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView2))->EndInit();
			this->tabPage3->ResumeLayout(false);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView3))->EndInit();
			this->tabPage4->ResumeLayout(false);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView4))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
private: int FillDataSet(SqlConnection ^con,DataSet ^data,String ^sql,String ^tablename)
		 {//用SqlDataAdapter填充DataSet(连接数据源对象,DataSet对象,SQL语句,DataSet中新建的表名)
			 SqlDataAdapter^ adapter = gcnew SqlDataAdapter(sql, con);
			 adapter->MissingSchemaAction=MissingSchemaAction::AddWithKey;//保存主键、唯一关系
			 int count=adapter->Fill(data,tablename);//自动Open()和Close()SqlConnection对象
			 delete adapter;
			 return count;//返回得到的行数
		 }
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 textBox1->Text="SELECT 姓名,车间名 AS 车间 FROM 车间,工人,关系 WHERE 工人.工号=关系.工号 AND 车间.车间号=关系.车间号";
			 textBox1->SelectAll();

 			 connection=gcnew SqlConnection();
			 connection->ConnectionString="Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Documents and Settings\\Administrator\\My Documents\\li.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True";

			 customers = gcnew DataSet();

			 //利用SqlDataAdapter从数据源读入表格填充DataSet,外键关系要显示声明,见下
			 this->FillDataSet(connection,customers,"SELECT * FROM 工人","工人");
			 this->FillDataSet(connection,customers,"SELECT * FROM 车间","车间");
			 this->FillDataSet(connection,customers,"SELECT * FROM 关系","关系");

			 //添加外键关系
			 customers->Relations->Add(
				 "工人-关系",
				 customers->Tables["工人"]->Columns["工号"],
				 customers->Tables["关系"]->Columns["工号"]);
			 customers->Relations->Add(
				 "车间-关系",
				 customers->Tables["车间"]->Columns["车间号"],
				 customers->Tables["关系"]->Columns["车间号"]);

			 //利用DataSet填充三个显示控件dataGridView
			 dataGridView1->DataSource=customers->Tables["工人"];
			 dataGridView2->DataSource=customers->Tables["车间"];
			 dataGridView3->DataSource=customers->Tables["关系"];

/*
			 String^temp=customers->Tables["关系"]->Rows[0][0]->ToString();//具体的项

			 String ^temp1=customers->GetXml();//数据库表格的XML结构
			 String ^temp2=customers->GetXmlSchema();//关系的XML结构
			 int j=customers->Tables->Count;//j是得到表的数量
*/
		 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {//查询按钮
			 if(4 == customers->Tables->Count)
			 {//先清掉第4个表(即"查询"表)和显示它的视图控件
				 customers->Tables->Remove("查询");
				 dataGridView4->DataSource=nullptr;
				 dataGridView4->Rows->Clear();
				 dataGridView4->Columns->Clear();
			 }

			 this->FillDataSet(connection,customers,textBox1->Text,"查询");
			 dataGridView4->DataSource=customers->Tables["查询"];

			 tabControl1->SelectedIndex=3;//将"查询"结果显示设置成当前显示的页
		 }

private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {//提交修改按钮
			 SqlDataAdapter^ adapter1 = gcnew SqlDataAdapter("SELECT * FROM 工人",connection);
			 SqlCommandBuilder ^builder1 = gcnew SqlCommandBuilder(adapter1);
			 SqlDataAdapter^ adapter2 = gcnew SqlDataAdapter("SELECT * FROM 车间",connection);
			 SqlCommandBuilder ^builder2 = gcnew SqlCommandBuilder(adapter2);
			 SqlDataAdapter^ adapter3 = gcnew SqlDataAdapter("SELECT * FROM 关系",connection);
			 SqlCommandBuilder ^builder3 = gcnew SqlCommandBuilder(adapter3);
			 connection->Open();
			 //出于性能考虑建议不删除时DataSet^temp=customers->GetChanges();然后操作temp
			 adapter1->Update(customers,"工人");
			 adapter2->Update(customers,"车间");
			 adapter3->Update(customers,"关系");
			 connection->Close();
			 delete adapter1;
			 delete adapter2;
			 delete adapter3;
		 }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {//退出按钮
			 delete customers;
			 connection->Close();
			 delete connection;
			 this->Close();
		 }
};
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -