首页 > 代码库 > SqlManage.cs

SqlManage.cs

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;
 
namespace CS2013
{
    class SqlManage
    {
        public static string cnnstring = "Server=127.0.0.1;Database=test;Uid=sa;Pwd=6521249;";
        public static SqlConnection cnn = null;

        public static void TableChange(string sql)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sql, SqlManage.cnn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("操作成功");
                }
                else
                {
                    MessageBox.Show("操作失败!\n检查数据库是否有该记录");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        public static DataTable TableSelect(string sql)
        {
            try
            {
                DataTable table = new DataTable();
                SqlDataAdapter sdp = new SqlDataAdapter(sql,SqlManage.cnn);
                sdp.Fill(table);
                return table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

    public static void SetDataGridView(DataGridView DGV)
    {
      // 表头不可见
      DGV.RowHeadersVisible = false;
      // 不允许增加行
      DGV.AllowUserToAddRows = false;
      // 只读
      DGV.ReadOnly = true;
      // 选中全行
      DGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      // 不允许修改列宽
      DGV.AllowUserToResizeRows = false;
      // 不允许修改行高
      DGV.AllowUserToResizeColumns = false;
      // 扩展列宽
      DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
      // 扩展行高
      DGV.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
      // 自动换行
      DGV.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;
    }
    }
}

 

SqlManage.cs