新手请教:帮忙改下这个登录程序的错误!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == " " || textBox2.Text == " ")
{
MessageBox.Show( "请输入用户名和密码,然后再登录 ");
return;
}
string SQL = "select Power from ID where UserID = ";
SQL += textBox1.Text + " and Power = ' " + textBox2.Text.Trim() + " ' ";
string myConStr = "Persist Security Info=False;Initial Catalog=mydata; ";
myConStr += "Data Source=localhost;Integrated Security=SSPI; ";
SqlCommand myCom = null;
SqlConnection myCon = null;
try
{
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand(SQL, myCon);
//返回权限代码
int Power = (short)myCom.ExecuteScalar();
switch (Power)
{
case 2:
MenuItem4.Enabled = true;
MenuItem6.Enabled = true;
MenuItem8.Enabled = true;
break;
case 1:
MenuItem5.Enabled = true;
MenuItem7.Enabled = true;
break;
default:
MessageBox.Show( "你没有使用本系统的权限 ");
break;
}
}
catch (SqlException oe)
{
MessageBox.Show(oe.Message, "Error ");
}
finally
{
if (myCon.State == ConnectionState.Open)
myCon.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
我刚学C#!请教下高手,我这个登录程序运行报错“列名Power无效”不知道问题在哪!怎么改!?
谢谢!
[解决办法]
可能是没有Power字段
上述代码存在若干问题
1.string SQL = "select Power from ID where UserID = ";
SQL += textBox1.Text + " and Power = ' " + textBox2.Text.Trim() + " ' "; //此行是否填写错误,textBox2输入abc ' or '1 '= '1就登录了
2、int Power = (short)myCom.ExecuteScalar();//当数据库中无记录时,出错
3、连接字符串最好统一管理
[解决办法]
string SQL = "select Power from ID where UserID = " + textBox1.Text + " and Power = ' " + textBox2.Text.Trim() + " ' ";
请检查数据库是否有Power字段
[解决办法]
string SQL = "select Power from ID where UserID = ' "+ textBox1.Text.Trim()+ " ' and 密码 = ' "+ textBox2.Text.Trim() + " ' ";
[解决办法]
string SQL = "select Power from ID where UserID = ";
SQL += textBox1.Text + " and Power = ' " + textBox2.Text.Trim() + " ' ";
好象错了吧
安全起见不要换行整理一下吧.