111,126
社区成员
发帖
与我相关
我的任务
分享 string sql="select count(*) from UU where USER='"+ namatext.Text.Trim()+ "'and PSW='"+ pswtext.Text.Trim() +"'";
//用占位符来
string sql=string.Format("select count(*) from UU where USER='{0}' and PSW='{1}'",namatext.Text.Trim(),pswtext.Text.Trim());
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;
namespace sqlconnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (namatext.Text.Trim() == "" || pswtext.Text.Trim() == "")
MessageBox.Show("请输入用户名和密码", "提示");
else
{
string Afile = "server=LAIPAN-B0559C3A;uid=sa;pwd=123;database=user";
SqlConnection AconnStr = new SqlConnection(Afile);
AconnStr.Open();
//你sql这里就有问题
string sql="select count(*) from UU where USER='"+ namatext.Text.Trim()+ "'and PSW='"+ pswtext.Text.Trim() +"'";
SqlCommand cmd=new SqlCommand(sql,AconnStr);
int count=0;
count=(int)cmd.ExecuteScalar();//执行查询语句
if(count==1){
MessageBox.Show("成功!")
this.Visible=false;
Form2 form2=new Form2();
form2.Show();
}
else
{
MessageBox.Show("用户名或者密码错误", "警告");
namatext.Text = "";
pswtext.Text="";
namatext.Focus();
}
catch (Exception ex)
{
MessageBox.Show("出错!")
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}