C#连接数据库时,报错“用户'sa'登录失败。该用户与可信SQL Server连接无关联”,直接用sa用户登陆SQL没问
代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;us...
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace Myschool
{
class MyConnection
{
public static SqlConnection myconn = new SqlConnection("server=.\\sqlexpress;database=MySchool;uid=sa;pwd=123");
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Myschool
{
public partial class Login : Form
{
public string UserType = string.Empty; //获取登录的类型
public string LoginName = string.Empty; //获取登录的名字
public Login()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (DBHelp(user.Text, password.Text, type.Text))
{
this.DialogResult = DialogResult.Yes;
if (type.Text == "管理员") { UserType = "管理员"; }
if (type.Text == "教师") { UserType = "教师"; }
if (type.Text == "学生") { UserType = "学生"; }
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入;");
}
}
public bool DBHelp(string LoginName,string LoginPwd,string LoginType)
{
try
{
MyConnection.myconn.Open();
string s = "";
if (LoginType == "管理员")
{
s = string.Format("select count(*) from Admin where LoginId='{0}' and LoginPwd='{1}'", LoginName, LoginPwd);
}
else if (LoginType == "教师")
{
s = string.Format("select count(*) from Teacher where LoginId='{0}' and LoginPwd='{1}'", LoginName, LoginPwd);
}
else if (LoginType == "学生")
{
s = string.Format("select count(*) from Student where LoginId='{0}' and LoginPwd='{1}'", LoginName, LoginPwd);
}
SqlCommand mycom = new SqlCommand(s, MyConnection.myconn);
int i = (int)mycom.ExecuteScalar();
if (i >= 0)
{
return true;
}
else
return false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
finally
{
MyConnection.myconn.Close();
}
}
private void Login_Load(object sender, EventArgs e)
{
}
private void cancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
} 展开
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace Myschool
{
class MyConnection
{
public static SqlConnection myconn = new SqlConnection("server=.\\sqlexpress;database=MySchool;uid=sa;pwd=123");
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Myschool
{
public partial class Login : Form
{
public string UserType = string.Empty; //获取登录的类型
public string LoginName = string.Empty; //获取登录的名字
public Login()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (DBHelp(user.Text, password.Text, type.Text))
{
this.DialogResult = DialogResult.Yes;
if (type.Text == "管理员") { UserType = "管理员"; }
if (type.Text == "教师") { UserType = "教师"; }
if (type.Text == "学生") { UserType = "学生"; }
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入;");
}
}
public bool DBHelp(string LoginName,string LoginPwd,string LoginType)
{
try
{
MyConnection.myconn.Open();
string s = "";
if (LoginType == "管理员")
{
s = string.Format("select count(*) from Admin where LoginId='{0}' and LoginPwd='{1}'", LoginName, LoginPwd);
}
else if (LoginType == "教师")
{
s = string.Format("select count(*) from Teacher where LoginId='{0}' and LoginPwd='{1}'", LoginName, LoginPwd);
}
else if (LoginType == "学生")
{
s = string.Format("select count(*) from Student where LoginId='{0}' and LoginPwd='{1}'", LoginName, LoginPwd);
}
SqlCommand mycom = new SqlCommand(s, MyConnection.myconn);
int i = (int)mycom.ExecuteScalar();
if (i >= 0)
{
return true;
}
else
return false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
finally
{
MyConnection.myconn.Close();
}
}
private void Login_Load(object sender, EventArgs e)
{
}
private void cancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
} 展开
4个回答
展开全部
你确信连接字符串没写错,你有装sqlserver其他版本吗
你确定要加\sqlexpress
你确定要加\sqlexpress
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
server=localhost试试看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我是用其他语言的 不知道你这个里面有没有类似loginprimit之类的设置 你可以看一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询