//以下为Mymeans文件
class Mymeans
{
public static SqlConnection My_con;
public static string Login_ID = "";
public static string Login_Name = "";
public static string M_str_sqlcon = "Data Source=5DF762D73A1A427;Database=PMS;User id=sa;Pwd=sa";
public static int Login_n = 0;
public static string AllSql ="Select * From 操作用户";
public static SqlConnection getcon()
{
My_con = new SqlConnection(M_str_sqlcon);
My_con.Open();
return My_con;
} public void con_close()
{
if (My_con.State == ConnectionState.Open)
{
My_con.Close();
My_con.Dispose();
}
} public SqlDataReader getcom(string SQLstr)
{
getcon();
SqlCommand My_com = My_con.CreateCommand();
My_com.CommandText = SQLstr;
SqlDataReader My_read = My_com.ExecuteReader();
return My_read;
} public void getsqlcom(string SQLstr)
{
getcon();
SqlCommand SQLcom = new SqlCommand(SQLstr,My_con);
SQLcom.ExecuteNonQuery();
SQLcom.Dispose();
con_close();
} public DataSet getDataSet(string SQLstr, string tableName)
{
getcon();
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con);
DataSet My_DataSet = new DataSet();
SQLda.Fill(My_DataSet, tableName);
con_close();
return My_DataSet;
}
}
//以下为F_Login文件
public partial class F_Login : Form
{
public F_Login()
{
InitializeComponent();
} PMS.Mymeans MyClass = new Mymeans();
//private int MyTryCount = 0; private void butLogin_Click(object sender, EventArgs e)
{
if (textName.Text != "" & textPass.Text != "")
{
SqlDataReader temDR = MyClass.getcom("Select * From 操作用户 Where 用户名称='"+textName.Text.Trim()+ "'and 用户密码='"+textPass.Text.Trim()+"'");
bool ifcom = temDR.Read();
if (ifcom)
{
PMS.Mymeans.Login_Name = textName.Text.Trim();
PMS.Mymeans.Login_ID = temDR.GetString(0);
PMS.Mymeans.My_con.Close();
PMS.Mymeans.My_con.Dispose();
this.Close();
}
else
{
// SqlCommand SQLcom1 = MyClass.getsqlcom();
// int MyCount = (int)SQLcom1.ExecuteScalar();
// if (MyCount == 1)
// {
// this.Close();
// }
// else
// {
// this.MyTryCount += 1;
// if (this.MyTryCount != 3)
// {
// int MyDoCount = 3 - this.MyTryCount;
// MessageBox.Show("用户密码或用户名称有错,还有" + MyDoCount + "次机会!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// this.textName.Text = "";
// this.textPass.Text = "";
// }
// else
// {
// butClose_Click(null, null);
// }
// }
MessageBox.Show("用户名或密码输入错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
textName.Text = "";
textPass.Text = "";
}
MyClass.con_close();
}
else
MessageBox.Show("请将登录信息添写完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void textName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
textPass.Focus();
} private void textPass_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
butLogin.Focus();
} private void butClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
这段代码主要目的是想,当用户登录时,如果输入错误超过三次,程序自动关闭.
在Mymeans中,写了SQL的链接方面的内容
在F_Login主要是实现用户登录我想在F_Login中加入"//"注释内的内容(红色部分),但在运行的时候,就会报错,说是CONNECTION属性错误.
不知道怎么改才能运行成功
class Mymeans
{
public static SqlConnection My_con;
public static string Login_ID = "";
public static string Login_Name = "";
public static string M_str_sqlcon = "Data Source=5DF762D73A1A427;Database=PMS;User id=sa;Pwd=sa";
public static int Login_n = 0;
public static string AllSql ="Select * From 操作用户";
public static SqlConnection getcon()
{
My_con = new SqlConnection(M_str_sqlcon);
My_con.Open();
return My_con;
} public void con_close()
{
if (My_con.State == ConnectionState.Open)
{
My_con.Close();
My_con.Dispose();
}
} public SqlDataReader getcom(string SQLstr)
{
getcon();
SqlCommand My_com = My_con.CreateCommand();
My_com.CommandText = SQLstr;
SqlDataReader My_read = My_com.ExecuteReader();
return My_read;
} public void getsqlcom(string SQLstr)
{
getcon();
SqlCommand SQLcom = new SqlCommand(SQLstr,My_con);
SQLcom.ExecuteNonQuery();
SQLcom.Dispose();
con_close();
} public DataSet getDataSet(string SQLstr, string tableName)
{
getcon();
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con);
DataSet My_DataSet = new DataSet();
SQLda.Fill(My_DataSet, tableName);
con_close();
return My_DataSet;
}
}
//以下为F_Login文件
public partial class F_Login : Form
{
public F_Login()
{
InitializeComponent();
} PMS.Mymeans MyClass = new Mymeans();
//private int MyTryCount = 0; private void butLogin_Click(object sender, EventArgs e)
{
if (textName.Text != "" & textPass.Text != "")
{
SqlDataReader temDR = MyClass.getcom("Select * From 操作用户 Where 用户名称='"+textName.Text.Trim()+ "'and 用户密码='"+textPass.Text.Trim()+"'");
bool ifcom = temDR.Read();
if (ifcom)
{
PMS.Mymeans.Login_Name = textName.Text.Trim();
PMS.Mymeans.Login_ID = temDR.GetString(0);
PMS.Mymeans.My_con.Close();
PMS.Mymeans.My_con.Dispose();
this.Close();
}
else
{
// SqlCommand SQLcom1 = MyClass.getsqlcom();
// int MyCount = (int)SQLcom1.ExecuteScalar();
// if (MyCount == 1)
// {
// this.Close();
// }
// else
// {
// this.MyTryCount += 1;
// if (this.MyTryCount != 3)
// {
// int MyDoCount = 3 - this.MyTryCount;
// MessageBox.Show("用户密码或用户名称有错,还有" + MyDoCount + "次机会!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// this.textName.Text = "";
// this.textPass.Text = "";
// }
// else
// {
// butClose_Click(null, null);
// }
// }
MessageBox.Show("用户名或密码输入错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
textName.Text = "";
textPass.Text = "";
}
MyClass.con_close();
}
else
MessageBox.Show("请将登录信息添写完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void textName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
textPass.Focus();
} private void textPass_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
butLogin.Focus();
} private void butClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
这段代码主要目的是想,当用户登录时,如果输入错误超过三次,程序自动关闭.
在Mymeans中,写了SQL的链接方面的内容
在F_Login主要是实现用户登录我想在F_Login中加入"//"注释内的内容(红色部分),但在运行的时候,就会报错,说是CONNECTION属性错误.
不知道怎么改才能运行成功
解决方案 »
- 已知公钥,求C#加密算法
- 关于在.net中用DotFuscator混淆DLL的问题?
- C# 如何截取字符串。。。 刚刚提问过了,可能我急着结账 答案没对。
- 每次取 ExtractIconEx 第十次的时候就会报错
- try catch处理异常,已经知道会有NULL异常发生,现在只想处理除NULL异常外的其它异常,也就是对NULL异常视而不见,在catch块中要怎样写?
- 请教一个接收POST数据是中文乱码的问题
- 求此正则表达式,急!!!!!!!!!!!
- 请教C#的调试问题...
- 百思不得其解,很奇怪的问题。
- BATA2到底比BATA1好了还是烦琐了!
- winform怎么保存设置,紧急求助
- table 问题
输入错误 一次加1,
大于三次就退出 int MyCount = (int)SQLcom1.ExecuteScalar();
ExecuteScalar只是返回首行首列,你的表结构不知道什么样,也不知道你这个地方要干嘛
通常我见人家设计的,都是将SQL与用户登录页面放在一起的,但我想将这两部分的代码分开.但系当我将那个大于三次就退出的部分放入在用户登录页面的时候,就无办法运行,
当我打入第一次错误时,需要将这个错误的记录返回给数据库,所以需要调用到数据,
这是我个人的理解,不知道有没有错.如果那段代码有错,那要怎样改才能最终实现这个效果呢?
下面加个int变量用来计数,当等于三时就让程序退出,Application.Exit();应该可以,你试试
2、所有详细代码:(注意i是全局变量)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace ewgerdf
{
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
}
int i = 3;
private void button1_Click(object sender, EventArgs e)
{
string infor = textBox1.Text.Trim(); if (textBox1.Text.Trim() != "123"&&i!=1&&infor!="")
{
--i;
label1.Text = "输入错误,你还有" + i + "次机会";
textBox1.SelectAll();
textBox1.Focus();
textBox1_TextChanged(null, null);
}
else if (textBox1.Text.Trim() == "123")
{ MessageBox.Show("输入成功");
}
else if (i == 1&& infor != "")
{
label1.Text = "输入错误,你还有" + "0次机会";
textBox1.Enabled = false; }
else
{
MessageBox.Show("输入用户名/密码!");
textBox1.Focus();
}
} private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
static int i=0;SqlDataReader temDR = MyClass.getcom("Select * From 操作用户 Where 用户名称='"+textName.Text.Trim()+ "'and 用户密码='"+textPass.Text.Trim()+"'");
bool ifcom = temDR.Read();
if (ifcom)
{
PMS.Mymeans.Login_Name = textName.Text.Trim();
PMS.Mymeans.Login_ID = temDR.GetString(0);
PMS.Mymeans.My_con.Close();
PMS.Mymeans.My_con.Dispose();
this.Close();
} else{messbox.show("shibai");i++}if(i==3)
{
application.exit();
}