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();
}
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();
}
{
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";
{
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("不存在");
}
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("不存在");
}
}
}
}
}
}