
2、引用下载后的mysql.data.dll
3、程序开始加:using MySql.Data.MySqlClient
4、连接数据库:
private void button1_Click(object sender, EventArgs e)//登入按钮
{
string power = comboBox1.Text.Trim()
string user = textBox1.Text.Trim()
string psd = textBox2.Text.Trim()
string ipaddress = ""
string mysqluser = ""
string mysqlpsd = ""
if (user == "")
{
MessageBox.Show("请输入用户名")
}
else if (psd == "")
{
MessageBox.Show("请输入密码")
}
else
{
try
{
try
{
string[] getconfig = File.ReadAllLines("E:/project/configure.txt", Encoding.GetEncoding("gb2312"))
ipaddress = getconfig[0].Split(':')[1]//读取ip地址
mysqluser = getconfig[1].Split(':')[1]//读取数据库账号
mysqlpsd = getconfig[2].Split(':')[1]//读取数据库密码
}
catch (Exception)
{
MessageBox.Show("配置文件丢失")
return
}
string query = "SET names gb2312SELECT COUNT(id) FROM fx_user WHERE name='" + user + "' AND password=MD5('" + psd + "') AND userid='" + power + "'"
MySqlConnection cn = new MySqlConnection("server=" + ipaddress + "user id=" + mysqluser + "Password=" + mysqlpsd + "database=systemcharset=gb2312")
cn.Open()
MySqlCommand cm = new MySqlCommand(query, cn)
MySqlDataReader read = cm.ExecuteReader() //搜索满足 用户名,密码, *** 作员的记录。
//如果记录没有-->密码或用户名错误
if (read.Read())//如果记录多余1条-->数据错误,联系管理员
{ //只有一条记录则成功登入
int x = Int32.Parse(read[0].ToString())
if (x == 0)
{
MessageBox.Show("用户名或密码错误")
}
else if (x >1)
{
MessageBox.Show("用户冲突,请联系管理员")
}
else if (x == 1)
{
// MessageBox.Show("登入成功")
main mf = new main(power, ipaddress, mysqluser, mysqlpsd) //将 *** 作员 和 IP地址传入 主窗体
mf.Show()
this.Hide()
cn.Close()
}
}
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("数据库连接失败1")
break
case 1045:
MessageBox.Show("数据库密码或用户名错误")
break
default:
MessageBox.Show("数据库连接失败2")
break
}
}
}
}
数据层还需要函数,否则你这个结构就不叫三层结构了,你这个函数应该是在数据访问层里到,数据访问层返回给逻辑层的数据不应该是SqlDataReader对象,你可以选择返回String[],List<....>,HashTable等。数据访问层:
给你几个我的函数吧:
public
static
void
Open()
{
if
(con.State
==
ConnectionState.Closed)
{
con.Open()
}
}
public
static
void
Close()
{
if
(con.State
==
ConnectionState.Open)
{
con.Close()
}
}
public
static
SqlCommand
CreateCommand(string
sql)
{
return
new
SqlCommand(sql,
con)
}
public
static
List<string>
RunSQLReturnList(string
sql)
{
List<string>
info
=
new
List<string>()
SqlCommand
cmd
=
CreateCommand(sql)
Open()
SqlDataReader
sdr
=
cmd.ExecuteReader()
while
(sdr.Read())
{
for
(int
i
=
0
i
<
sdr.FieldCount
i++)
{
info.Add(sdr.GetName(i).ToString())
info.Add(sdr.GetValue(i).ToString())
}
}
Close()
return
info
}
public
static
Hashtable
RunSQLReturnHashTable(string
sql)
{
List<string>
lt
=
RunSQLReturnList(sql)
Hashtable
ht
=
new
Hashtable()
for
(int
i
=
0
i
<
lt.Count
i
+=
2)
{
ht[lt[i]]
=
lt[i
+
1]
}
return
ht
}
数据访问层返回给逻辑层HashTable对象hashTable1:
user.username=hashTable1["Name"].ToString()
.........
逻辑层返回给表示层实体对象user
label1.text
=
user.UserName
一下代码都是在服务器端写.cs文件
page1
session["id"]
=
"101"
page1
session["id"]
=
"102"
response.redirect("page2.aspx?id="
+
session["id"]
)
page2
page_load
{
string
id
=
request.querystring["id"]
string
sql
=
"select
??from
??where
id="+id
label赋值
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)