• 大小: 45KB
    文件类型: .rar
    金币: 2
    下载: 2 次
    发布日期: 2021-11-08
  • 语言: C#
  • 标签: C#  SQL  DataGridView  

资源简介

C#实现将SQL数据库的内容读取出来显示到DataGridview中,同时根据DataGridview更新数据库内容

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Collections;

namespace SQL
{
    public partial class Form1 : Form
    {

        SqlDataAdapter sa = null;
        SqlCommandBuilder sb = null;
        DataTable dt = null;
        BindingSource bs = null; 

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {
           // ArrayList DBDataArray = new ArrayList();

            string connString = “Server=localhost;Database=MySQL;Integrated Security = SSPI;“;
            
            SqlConnection conn = new SqlConnection(connString);
            

            //string sqlStatement = “select * from Infomation“;

            //SqlCommand sqlcmd = new SqlCommand(sqlStatement conn);            //设置参数
            try 
            { 
                conn.Open();

                //SqlDataReader sdr = sqlcmd.ExecuteReader(); //执行SQL语句
                //int cols = sdr.FieldCount;   //获取结果行中的列数
                                     
                //while (sdr.Read())
                //{
                //    object[] values = new object[cols];
                //    sdr.GetValues(values);       //values保存一行数据

                //    DBDataArray.Add(values);
               
                //}
                //sdr.Close();
                string sqlCheckStr = “select * from Infomation“;
                sa = new SqlDataAdapter(sqlCheckStr conn);

                sb = new SqlCommandBuilder(sa);//绑定SqlDataAdapter 

                dt = new DataTable();

                sa.Fill(dt);

                dataGridView1.DataSource = dt;
                bs = new BindingSource();

                bs.DataSource = dt; //绑定BindingSource 

                conn.Close();


                // 设定单元格的ToolTip内容
                dataGridView1.Columns[0].ToolTipText = “该列单元格的内容不能为空“;
            }   

            catch (Exception ex)
            {
                conn.Close();
                throw ex;
            } 

        }


        private void button2_Click(object sender EventArgs e)
        {
            try
            {
                if ( null == sa )
                {
                    MessageBox.Show(“请先打开数据库“);
                    return;
                }
                sa.Update((DataTable)bs.DataSource);
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show(“DataSet 无效“);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show(“源表 无效“);
            }
            catch (DBConcurrencyException)
            {
                MessageBox.Show(“试图执行 INSERT、UPDATE 或 DELETE 语句,但没有记录受到影响“);
   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      12288  2009-07-08 23:35  SQL\SQL\bin\Debug\SQL.exe

     文件      32256  2009-07-08 23:35  SQL\SQL\bin\Debug\SQL.pdb

     文件      14328  2009-07-08 23:37  SQL\SQL\bin\Debug\SQL.vshost.exe

     文件        490  2007-07-21 01:33  SQL\SQL\bin\Debug\SQL.vshost.exe.manifest

     文件      20480  2009-07-03 08:25  SQL\SQL\Database1.sdf

     文件       4761  2009-07-08 23:34  SQL\SQL\Form1.cs

     文件       5394  2009-07-08 23:33  SQL\SQL\Form1.Designer.cs

     文件       5814  2009-07-08 23:33  SQL\SQL\Form1.resx

     文件        630  2009-07-08 23:35  SQL\SQL\Form2.cs

     文件       2627  2009-07-08 23:35  SQL\SQL\Form2.Designer.cs

     文件       5814  2009-07-08 23:35  SQL\SQL\Form2.resx

     文件        460  2009-07-08 23:37  SQL\SQL\obj\Debug\SQL.csproj.FileListAbsolute.txt

     文件        905  2009-07-08 23:35  SQL\SQL\obj\Debug\SQL.csproj.GenerateResource.Cache

     文件      12288  2009-07-08 23:35  SQL\SQL\obj\Debug\SQL.exe

     文件        180  2009-07-08 23:34  SQL\SQL\obj\Debug\SQL.Form1.resources

     文件        180  2009-07-08 23:35  SQL\SQL\obj\Debug\SQL.Form2.resources

     文件      32256  2009-07-08 23:35  SQL\SQL\obj\Debug\SQL.pdb

     文件        180  2009-07-08 23:34  SQL\SQL\obj\Debug\SQL.Properties.Resources.resources

     文件        484  2009-06-30 09:42  SQL\SQL\Program.cs

     文件       1364  2009-06-30 09:42  SQL\SQL\Properties\AssemblyInfo.cs

     文件       2856  2009-06-30 09:42  SQL\SQL\Properties\Resources.Designer.cs

     文件       5612  2009-06-30 09:42  SQL\SQL\Properties\Resources.resx

     文件       1088  2009-06-30 09:42  SQL\SQL\Properties\Settings.Designer.cs

     文件        249  2009-06-30 09:42  SQL\SQL\Properties\Settings.settings

     文件       5758  2009-07-08 23:33  SQL\SQL\SQL.csproj

     文件        486  2009-07-03 08:38  SQL\SQL\SQL.csproj.user

     文件        899  2009-06-30 09:42  SQL\SQL.sln

    ..A..H.     16384  2009-07-08 23:44  SQL\SQL.suo

     目录          0  2009-06-30 09:42  SQL\SQL\obj\Debug\TempPE

     目录          0  2009-07-08 23:34  SQL\SQL\bin\Debug

............此处省略10个文件信息

评论

共有 条评论