zoukankan      html  css  js  c++  java
  • C#连接数据库的方法

    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;
    namespace 登录
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void btnno_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            private void btnyes_Click(object sender, EventArgs e)
            {
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                scsb.DataSource = "127.0.0.1";
                scsb.UserID = "sa";
                scsb.Password = "123456";
                scsb.InitialCatalog = "studb";
                SqlConnection conn = new SqlConnection(scsb.ToString());//参数:连接数据库的字符串
                if(conn.State==System.Data.ConnectionState.Closed)//判断连接的开启状态
                conn.Open();
                string userid=txtUser.Text.Trim();
                string owd = txtPwd.Text.Trim();
                string strSQL = "select owd from userinfo where [userid]='"+userid+"'";
                SqlCommand comm = new SqlCommand(strSQL,conn);
                SqlDataReader dr = comm.ExecuteReader();
                if(dr.Read())
                {
                    if ((string)dr["owd"] == owd)
                        MessageBox.Show("成功");
                    else
                        MessageBox.Show("错误");
                }
                else
                {
                    MessageBox.Show("不存在");
                }
            }
        }
    }
  • 相关阅读:
    Linux手动分区步骤
    Vue到底是怎样个框架?
    MongoDB
    25、正则表达式
    24、模块
    21、三元表达式、列表解析、生成器
    Linux 软件包 管理
    CentOS7.5---7.9 中文字体匹配错误 fontconfig-2.13.0
    Ubuntu14.04下Git安装与使用
    Zabbix3.4 安装配置
  • 原文地址:https://www.cnblogs.com/lxabner/p/12322443.html
Copyright © 2011-2022 走看看