c# 连接数据库

本人懒惰,不想了解那么杂七杂八的东西,写个通用连库文件,以后连库直接粘贴复制就好了

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace sql连接数据库demo
{
    public partial class Form1 : Form
    {
        public string connStr = "Data Source=LIUHENG\\SQL2005L;Initial Catalog=webTest;User ID=sa;Password=liuheng0429";
        public SqlConnection conn;
        public SqlCommand cmd;
        public Form1()
        {
            InitializeComponent();
            this.Text = "sql连接数据库";
        }

        private void button1_Click(object sender, EventArgs e)
        {


            if (textBox1.Text != "")
            {
                string sqlText = "insert into house(typeS) values('" + textBox1.Text + "')";

                int i = ExecuteNonQuery(sqlText);

                if (i > 0)
                {
                    MessageBox.Show("添加完成");
                    dataGridView1.DataSource = GetData().DefaultView;
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

            dataGridView1.DataSource = GetData().DefaultView;
        }


        private DataTable GetData()
        {
            return GetTable("select * from house order by id desc ");


        }

        #region 数据库操作
        /// <summary>
        /// 打开链接
        /// </summary>
        /// <returns></returns>
        private SqlConnection GetConn()
        {
            if (conn == null) conn = new SqlConnection(connStr);
            if (conn.State == ConnectionState.Closed) conn.Open();
            else if (conn.State == ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();
            }

            return conn;
        }

        /// <summary>
        /// 执行增删改查操作
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private int ExecuteNonQuery(string sql)
        {
            try
            {
                cmd = new SqlCommand(sql, GetConn());
                return cmd.ExecuteNonQuery();
            }
            catch
            {
                return 0;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 读数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private SqlDataReader ExecuteReader(string sql)
        {
            try
            {
                cmd = new SqlCommand(sql, GetConn());
                return cmd.ExecuteReader();
            }
            catch
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 得到该表数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private DataTable GetTable(string sql)
        {
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, GetConn());
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds.Tables[0];
            }
            catch
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {

                int i = ExecuteNonQuery("delete house where id = " + textBox2.Text);
                if (i > 0)
                {
                    MessageBox.Show("删除成功");
                    dataGridView1.DataSource = GetData().DefaultView;
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }
    }

}

 


现在有个新的需求:

dataGridView  里面 修改数据

然后,点击按钮更新数据

 

posted @ 2012-04-19 22:56  小四儿  阅读(241)  评论(0编辑  收藏  举报