WinForm数据库操作

连接数据库并且导入数据的简单步骤

SqlConnection lo_conn = new SqlConnection("Data Source=MSI-PC;Initial Catalog=进销存;Integrated Security=True");//连接数据库过程

lo_conn.Open();
SqlCommand lo_cmd = new SqlCommand();   //创建命令对象
lo_cmd.CommandText = "select 商品编号,商品名称,售价 from Goods where 商品编号=" + txtId.Text;//ID查找
lo_cmd.Connection = lo_conn;
SqlDataAdapter adapter = new SqlDataAdapter(lo_cmd);//创建adapter对象

adapter.Fill(dt);

dgvPay.DataSource = dt;

lo_conn.Close();//关闭数据库

DBHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Shop_System
{
    /// 
    /// 数据库操作的类
    /// 
    public class DBHelper
    {


        private static string DBName = RWAppConfig.ReadAppconfig("DBName");
        private static string DBURL = RWAppConfig.ReadAppconfig("DBURL");
        private static string DBUser = RWAppConfig.ReadAppconfig("DBUser");
        private static string DBPassword = RWAppConfig.ReadAppconfig("DBPassword");

        private static string connstr = "Initial Catalog = " + DBName + ";Data Source=" + DBURL + ";User ID=" + DBUser + ";Password=" + DBPassword;

        /// 
        /// 从数据库里获得数据
        /// 
        /// sql语句
        /// 返回结果DataSet;出错则返回null
        public static DataSet GetData(string sqlstr, out int returncode, out Exception sql_err)
        {
            DataSet ds = null;
            try
            {
                using (SqlConnection conn = new SqlConnection(connstr))
                {

                    conn.Open();
                    SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
                    ds = new DataSet();
                    sda.Fill(ds);
                    conn.Close();
                    returncode = ds.Tables[0].Rows.Count;
                    sql_err = null;
                }
            }
            catch (Exception e)
            {
                returncode = -1;
                sql_err = e;
                return null;
            }

            return ds;

        }


        /// 
        /// 执行insert,update类型的sql语句
        /// 
        /// sql语句
        /// 0-执行正确;-1-出错
        public static int RunSqlCommand(string sqlstr)
        {

            using (SqlConnection conn = new SqlConnection(connstr))
            {
                SqlCommand sc = conn.CreateCommand();
                sc.CommandText = sqlstr;
                conn.Open();
                //建立事务
                SqlTransaction st = conn.BeginTransaction();
                sc.Transaction = st;
                try
                {
                    sc.ExecuteNonQuery();
                    st.Commit();
                    conn.Close();
                }
                catch (Exception)
                {
                    st.Rollback();
                    conn.Close();
                    return -1;
                }

            }

            return 0;

        }

        public static string GetMc(string bhbm, string bh, out int returncode)
        {
            string mc = "";
            string sql = "select mc from " + bhbm + " where bh = " + bh;
            returncode = 0;
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                SqlCommand sqlcmd = new SqlCommand(sql, conn);
                try
                {
                    conn.Open();
                    mc = sqlcmd.ExecuteScalar().ToString();
                }
                catch (Exception)
                {
                    returncode = -1;
                    return "";
                }
            }
            returncode = 0;
            return mc;

        }

        public static int GetBh(string bhbm, string mc, out int returncode)
        {
            int bh = 0;
            string sql = "select mc from " + bhbm + " where trim(mc) = '" + mc.Trim() + "'";
            returncode = 0;
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                SqlCommand sqlcmd = new SqlCommand(sql, conn);
                try
                {
                    conn.Open();
                    bh = Convert.ToInt32(sqlcmd.ExecuteScalar().ToString());
                }
                catch (Exception)
                {
                    returncode = -1;
                    return 0;
                }
            }
            returncode = 0;
            return bh;

        }


    }
}

RWAppConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace Shop_System
{
    public class RWAppConfig
    {
        public static string ReadAppconfig(string keyname)
        {
            return ConfigurationManager.AppSettings[keyname];
        }
        public static void WriteAppconfig(string kyname, string keyvalue)
        {

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[kyname].Value = keyvalue;
            config.Save();

        }
        public static void AddKeyValue(string keyname, string keyvalue)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Add("key", "Name");
            config.Save();
        }
    }

}

你可能感兴趣的:(C#)