⑵ 基于SQL Server数据库 using namespace System::Data::SqlClient;(建立连接必考) String^ Source=L\ L\ L\ SqlConnection^ conn=gcnew SqlConnection(Source); conn->Open(); 其中,连接的数据库服务器是SQL Server,这里采用默认方式。 步骤1:建立SQL Server数据库StudentDb和slist表 步骤2:建立一个CLR之Form项目 步骤3:手工添加浏览数据库表slist的实现代码 void ExecuteNonQueryTest() { String^ update=L\姓名='刘邦' WHERE 姓名='项羽'\ String^ Source=L\ +L\ SqlConnection^ conn=gcnew SqlConnection(Source); conn->Open(); SqlCommand^ Cmd=gcnew SqlCommand(update,conn); int rint=Cmd->ExecuteNonQuery(); Console::WriteLine(L\ conn->Close(); } void ExecuteReaderTest() { String^ select=L\ String^ Source=L\“ +L\ SqlConnection^ conn=gcnew SqlConnection(Source); conn->Open(); SqlCommand^ Cmd=gcnew SqlCommand(select,conn); SqlDataReader^ reader=Cmd->ExecuteReader(); while(reader->Read()) { Console::WriteLine( L\ reader[2], reader[3], reader[4]); } conn->Close();
11
} void ExecuteScalarTest() { String^ select=L\姓名FROM slist\ String^ Source=L\ +L\ SqlConnection^ conn=gcnew SqlConnection(Source); conn->Open(); SqlCommand^ Cmd=gcnew SqlCommand(select,conn); Object^ scalar=Cmd->ExecuteScalar(); Console::WriteLine(scalar); conn->Close(); }
12
13