Winform连接数据库

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
//using System.Configuration;

namespace winformlogin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            #region using连接数据库
            //string connStr = "Data Source=.\\SQLEXPRESS;Initial Catalog=MySchool;uid=sa;pwd=sa";
            //string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            string connStr = DBHelper.connStr;
            using (SqlConnection con = new SqlConnection(connStr))
            {
                //string sql = string.Format("select count(1) from student where LogInId=‘{0}‘ and LogInPwd=‘{1}‘",user.Text,pwd.Text);
                //SqlCommand cmd = new SqlCommand(sql,con);
                string sql = string.Format("select count(1) from student where LogInId=@LogInId and LogInPwd=@LogInPwd");
                SqlCommand cmd = new SqlCommand(sql,con);
                cmd.Parameters.AddWithValue("@LogInId",user.Text);
                cmd.Parameters.AddWithValue("@LogInPwd",pwd.Text);
                con.Open();
                int n = Convert.ToInt32(cmd.ExecuteScalar());
                if (n >= 1)
                {
                    Form2 fr2 = new Form2();
                    fr2.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("输入有误");
                }
            }
            #endregion

            #region 使用异常处理机制
            //string connStr = "Data Source=.\\SQLEXPRESS;Initial Catalog=MySchool;uid=sa;pwd=sa";
            //SqlConnection con = new SqlConnection(connStr);
            //try
            //{
            //    con.Open();
            //    MessageBox.Show("连接数据库");
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
            //finally
            //{
            //    con.Close();
            //}
            #endregion

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string connStr = "Data Source=.\\SQLEXPRESS;Initial Catalog=MySchool;uid=sa;pwd=sa";
            using (SqlConnection con = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand("select count(1) from Student", con);
                con.Open();
                int n = Convert.ToInt32(cmd.ExecuteScalar());
                MessageBox.Show(n.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


以下是DBHelper.cs 类

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

namespace winformlogin
{
    class DBHelper
    {
        public static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        public static string bankStr = ConfigurationManager.ConnectionStrings["bankStr"].ConnectionString;
    }
}

 以下是config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="bankStr" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Bank;uid=sa;pwd=sa" providerName="System.Data.SqlClient"/>
    <add name="connStr" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MySchool;uid=sa;pwd=sa" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

 

注意:还要在应用中引用 System.Configuration 引用

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。