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  里面 修改数据

然后,点击按钮更新数据

 

转载于:https://www.cnblogs.com/IRIS-TK1/archive/2012/04/19/2458568.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值