服务器名leslie-pc 数据库名db_11表名Table_1表项‘用户名’‘密码’,怎么实现登录按钮的功能,从数据库验证有没有该用户,还有注册按钮,弄了一下午没有弄好,就是数据库的查询和验证,最好给出能运行代码,数据库是SQl server
--------------------编程问答--------------------
--------------------编程问答--------------------
public partial class Form1 : Form
{
private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionString);
try {
string sql = "select * from Table_1 where Table_1.用户名=@username and Table_1.密码=@pwd";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@pwd", textBox2.Text);
con.Open();
//cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
string sql="select * from Table_1 where 用户名='"+this.textBox1.Text+"',密码='"+this.textBox2.Text+"'";
SqlCommand sqlcmd=new SqlCommand(sql,this.conn);
int i=sqlcmd.ExecuteNonQuery();
if(i!=0){判断用户名和密码}
换成:
string sql = "select * from Table_1 where Table_1.用户名=@username and Table_1.密码=@pwd";
SqlCommand cmd = new SqlCommand(sql, conn);
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;
using System.Configuration;
namespace WindowsApplication10
{
public partial class Form1 : Form
{
private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionString);
try
{
string sql = "select * from Table_1 where Table_1.用户名=@username and Table_1.密码=@pwd";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@pwd", textBox2.Text);
con.Open();
//cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
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;
using System.Configuration;
namespace WindowsApplication10
{
public partial class Form1 : Form
{
private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionString);
try
{
string sql = "select * from Table_1 where Table_1.用户名=@username and Table_1.密码=@pwd";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@pwd", textBox2.Text);
con.Open();
//cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);