C#连接数据库时,报错“用户'sa'登录失败。该用户与可信SQL Server连接无关联”,但sa用户直接SQL登陆可以
代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSyst...
代码如下:
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();
}
private void type_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
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");
}
}
用 sql server 2005 展开
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();
}
private void type_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
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");
}
}
用 sql server 2005 展开
展开全部
数据库服务没打开吧.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
服务器身份验证要选择sql server 和windows 身份验证模式
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
连接数据库的字符串写错了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlCommand mycom = new SqlCommand(s, MyConnection.myconn);
mycom.opne();
需要open啊
mycom.opne();
需要open啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询