65.9K
CodeProject 正在变化。 阅读更多。
Home

在 VC++.NET 中使用 DataGrid

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.20/5 (5投票s)

2004年12月16日

2分钟阅读

viewsIcon

106024

downloadIcon

2491

本文演示了 DataGrid、ADO.NET 组件,以及将数据存储到数据库并从数据库中检索数据的方法。

引言

这是我在学习 VC++ .NET 过程中的一点小小的贡献。这是一个非常简单且经常使用的场景:使用 DataGrid。在这里,我开发了一个小型应用程序,该应用程序创建数据库并存储数据(图像),并从数据库中检索数据并在 DataGrid 中显示它。

代码解释

要求

  • MS Visual Studio .NET 2003
  • Windows XP
  • SQL Server 2000

我使用 SQL Server 进行数据库操作。该应用程序展示了值(图像)如何存储在数据库中,以及当数据库中的更改发生时,网格如何更新。这是我的应用程序的快照。

Sample screenshot

使用的控件

  • Button:创建数据库、选择图像、保存到数据库
  • PictureBoxPictureBox1
  • DataGridDatagrid1

创建数据库

当您单击“创建数据库”按钮时,将在 SQL Server NorthWind 数据库中创建一个名为 Picture 的表。如果它已经存在,将被删除并重新创建。

String* strSQL = 
        S"IF EXISTS (SELECT * FROM northwind.dbo.sysobjects WHERE NAME " 
        "= 'Picture' AND TYPE = 'u')\r\nBEGIN\r\nDROP " 
        "TABLE northwind.dbo.picture\r\nEND\r\nCREATE TABLE " 
        "Picture (PictureID Int IDENTITY(1,1) NOT NULL,[FileName] " 
        "Varchar(255) NOT NULL,Picture Image NOT NULL," 
        "CONSTRAINT [PK_Picture] PRIMARY KEY CLUSTERED(PictureID))";
Try
{
    SqlConnection* conCreate = new SqlConnection(strconString);
    SqlCommand* sqlCmd = new SqlCommand(strSQL,conCreate);
    conCreate->Open();
    sqlCmd->ExecuteNonQuery();
    conCreate->Close();

      MessageBox::Show(S"Table is Created Successfully",S"Table   
        Creation",MessageBoxButtons::OK,MessageBoxIcon::Information);

}
catch(System::Exception* ee)
{
    MessageBox::Show(ee->Message,S"Error",MessageBoxButtons::OK,
                         MessageBoxIcon::Information);
}

将创建表的代码和声明适当变量的代码放在程序中。

选择图像

打开文件对话框用于选择特定的图像。当单击“选择图像”按钮时,将显示打开文件对话框以选择图像(BMP/GIF/JPG)。

   openFileDialog1->InitialDirectory = S"C:\\";
   openFileDialog1->Filter = S"All  
                       Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg";
   openFileDialog1->FilterIndex = 2;

   if(openFileDialog1->ShowDialog()== DialogResult::OK)
    {
    pictureBox1->Image = Image::FromFile(openFileDialog1->FileName);
    pictureBox1->SizeMode = PictureBoxSizeMode::CenterImage;
    pictureBox1->BorderStyle = BorderStyle::Fixed3D;
    lblImage_Path->Text = openFileDialog1->FileName;

    }

将代码放在“选择图像”按钮的 Click 事件中。

保存到数据库

当单击“保存到数据库”按钮时,所选图像将通过将其转换为字节流的方式存储到 Picture 表中。存储到数据库后,刷新 DataGrid

//To Convert the Image to Bytes
MemoryStream* mst = new MemoryStream();
pictureBox1->Image->Save(mst,pictureBox1->Image->RawFormat);
Byte BinaryImg[] = mst->GetBuffer();
mst->Close();

然后,将图像名称插入到数据库中。

//Insert into Table
conNwnd = new SqlConnection(strconString);
String* strSQL = S"INSERT INTO Picture" 
                  " (Filename, Picture)VALUES (@Filename, @Picture)";

//To Fill the DataGrid

sqlcmd = new SqlCommand(strSQL,conNwnd);
sqlcmd->Parameters->Add(new SqlParameter(S"@Filename", 
        SqlDbType::NVarChar,50))->Value = strImgFile[0];
sqlcmd->Parameters->Add(new SqlParameter(S"@Picture", 
        SqlDbType::Image))->Value = BinaryImg;

conNwnd->Open();
sqlcmd->ExecuteNonQuery();
conNwnd->Close();
RefreshTable();
MessageBox::Show(String::Concat(strImgFile[0],S" saved to Database."), 
       S"Image Status",MessageBoxButtons::OK,MessageBoxIcon::Information);

在这里,使用 RefreshTable 函数来刷新网格。

RefreshTable()
conNwnd = new SqlConnection(strconString);
SqlDataAdapter* sqlDA = new SqlDataAdapter(S"Select PictureID,Filename," 
                        "Picture FROM Picture",conNwnd);
TableData->Clear();
sqlDA->Fill(TableData,INPUT_TABLE);
dataGrid1->DataSource = TableData->Tables->Item[INPUT_TABLE];

在表单加载调用时,此 RefreshTable 函数将显示数据库中的值。

结论

通过这个小型应用程序,初学者可以理解 DataGrid 以及数据库连接,以及如何使用 VC++ .NET 将值存储和检索到数据库中。 欢迎您提出宝贵的意见,这将帮助我进一步提高我的知识水平。

© . All rights reserved.