MySql.Data.MySqlClient.MySqlException: Access denied for user “root”@”localhost” (using password: NO)
在 MySql.Data.MySqlClient.MySqlStream.OpenPacket()
在 MySql.Data.MySqlClient.NativeDriver.Authenticate411()
在 MySql.Data.MySqlClient.NativeDriver.Authenticate()
在 MySql.Data.MySqlClient.NativeDriver.Open()
在 MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
在 MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
在 MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
在 MySql.Data.MySqlClient.MySqlPool.GetConnection()
在 MySql.Data.MySqlClient.MySqlConnection.Open()
在 StockManageWebservice.DBOperation..ctor() 位置 E:\Coding\StockManageWebservice\StockManageWebservice\DBOperation.cs:行号 37
在 StockManageWebservice.Service1..ctor() 位置 E:\Coding\StockManageWebservice\StockManageWebservice\Service1.asmx.cs:行号 19
下面是代码:
类DBOperation:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
namespace StockManageWebservice
{
/// <summary>
/// 一个操作数据库的类,全部对SQLServer的操作都写在这个类中,使用的时候实例化一个然后直接调用就可以
/// </summary>
public class DBOperation : IDisposable
{
public static MySqlConnection sqlCon; //用于连接数据库
//将下面的引号之间的内容换成上面记录下的属性中的连接字符串
private String ConServerStr = @”server=localhost;User Id=root;database=stockmanage”;
//默认构造函数
public DBOperation()
{
if (sqlCon == null)
{
sqlCon = new MySqlConnection();
sqlCon.ConnectionString = ConServerStr;
sqlCon.Open();
}
}
//关闭/销毁函数,相当于Close()
public void Dispose()
{
if (sqlCon != null)
{
sqlCon.Close();
sqlCon = null;
}
}
/// <summary>
/// 获取全部货物的信息
/// </summary>
/// <returns>全部货物信息</returns>
public List<string> selectAllCargoInfor()
{
List<string> list = new List<string>();
try
{
string sql = “select * from c”;
MySqlCommand cmd = new MySqlCommand(sql, sqlCon);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//将结果集信息添加到返回向量中
list.Add(reader[0].ToString());
list.Add(reader[1].ToString());
list.Add(reader[2].ToString());
}
reader.Close();
cmd.Dispose();
}
catch (Exception)
{
}
return list;
}
/// <summary>
/// 增加一条货物信息
/// </summary>
/// <param name=”Cname”>货物名称</param>
/// <param name=”Cnum”>货物数量</param>
public bool insertCargoInfo(string Cname, int Cnum)
{
try
{
string sql = “insert into c (Cname,Cnum) values (“” + Cname + “”,” + Cnum + “)”;
MySqlCommand cmd = new MySqlCommand(sql, sqlCon);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 删除一条货物信息
/// </summary>
/// <param name=”Cno”>货物编号</param>
public bool deleteCargoInfo(string Cno)
{
try
{
string sql = “delete from c where Cno=” + Cno;
MySqlCommand cmd = new MySqlCommand(sql, sqlCon);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
}
}
Service1.asmx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace StockManageWebservice
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
DBOperation dbOperation = new DBOperation();
[WebMethod]
public string HelloWorld()
{
return “Hello World”;
}
[WebMethod(Description = “获取全部货物的信息”)]
public string[] selectAllCargoInfor()
{
return dbOperation.selectAllCargoInfor().ToArray();
}
[WebMethod(Description = “增加一条货物信息”)]
public bool insertCargoInfo(string Cname, int Cnum)
{
return dbOperation.insertCargoInfo(Cname, Cnum);
}
[WebMethod(Description = “删除一条货物信息”)]
public bool deleteCargoInfo(string Cno)
{
return dbOperation.deleteCargoInfo(Cno);
}
}
}
在 MySql.Data.MySqlClient.MySqlStream.OpenPacket()
在 MySql.Data.MySqlClient.NativeDriver.Authenticate411()
在 MySql.Data.MySqlClient.NativeDriver.Authenticate()
在 MySql.Data.MySqlClient.NativeDriver.Open()
在 MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
在 MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
在 MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
在 MySql.Data.MySqlClient.MySqlPool.GetConnection()
在 MySql.Data.MySqlClient.MySqlConnection.Open()
在 StockManageWebservice.DBOperation..ctor() 位置 E:\Coding\StockManageWebservice\StockManageWebservice\DBOperation.cs:行号 37
在 StockManageWebservice.Service1..ctor() 位置 E:\Coding\StockManageWebservice\StockManageWebservice\Service1.asmx.cs:行号 19
下面是代码:
类DBOperation:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
namespace StockManageWebservice
{
/// <summary>
/// 一个操作数据库的类,全部对SQLServer的操作都写在这个类中,使用的时候实例化一个然后直接调用就可以
/// </summary>
public class DBOperation : IDisposable
{
public static MySqlConnection sqlCon; //用于连接数据库
//将下面的引号之间的内容换成上面记录下的属性中的连接字符串
private String ConServerStr = @”server=localhost;User Id=root;database=stockmanage”;
//默认构造函数
public DBOperation()
{
if (sqlCon == null)
{
sqlCon = new MySqlConnection();
sqlCon.ConnectionString = ConServerStr;
sqlCon.Open();
}
}
//关闭/销毁函数,相当于Close()
public void Dispose()
{
if (sqlCon != null)
{
sqlCon.Close();
sqlCon = null;
}
}
/// <summary>
/// 获取全部货物的信息
/// </summary>
/// <returns>全部货物信息</returns>
public List<string> selectAllCargoInfor()
{
List<string> list = new List<string>();
try
{
string sql = “select * from c”;
MySqlCommand cmd = new MySqlCommand(sql, sqlCon);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//将结果集信息添加到返回向量中
list.Add(reader[0].ToString());
list.Add(reader[1].ToString());
list.Add(reader[2].ToString());
}
reader.Close();
cmd.Dispose();
}
catch (Exception)
{
}
return list;
}
/// <summary>
/// 增加一条货物信息
/// </summary>
/// <param name=”Cname”>货物名称</param>
/// <param name=”Cnum”>货物数量</param>
public bool insertCargoInfo(string Cname, int Cnum)
{
try
{
string sql = “insert into c (Cname,Cnum) values (“” + Cname + “”,” + Cnum + “)”;
MySqlCommand cmd = new MySqlCommand(sql, sqlCon);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 删除一条货物信息
/// </summary>
/// <param name=”Cno”>货物编号</param>
public bool deleteCargoInfo(string Cno)
{
try
{
string sql = “delete from c where Cno=” + Cno;
MySqlCommand cmd = new MySqlCommand(sql, sqlCon);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
}
}
Service1.asmx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace StockManageWebservice
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
DBOperation dbOperation = new DBOperation();
[WebMethod]
public string HelloWorld()
{
return “Hello World”;
}
[WebMethod(Description = “获取全部货物的信息”)]
public string[] selectAllCargoInfor()
{
return dbOperation.selectAllCargoInfor().ToArray();
}
[WebMethod(Description = “增加一条货物信息”)]
public bool insertCargoInfo(string Cname, int Cnum)
{
return dbOperation.insertCargoInfo(Cname, Cnum);
}
[WebMethod(Description = “删除一条货物信息”)]
public bool deleteCargoInfo(string Cno)
{
return dbOperation.deleteCargoInfo(Cno);
}
}
}
解决方案
20
Access denied for user “root”@”localhost” (using password: NO)
一般可解释为:用户名或口令错
root 是 MySQL 默认的管理员账号,不可能有误
所以很明显,您的 root 账号是需要口令的,但你并没有给出
一般可解释为:用户名或口令错
root 是 MySQL 默认的管理员账号,不可能有误
所以很明显,您的 root 账号是需要口令的,但你并没有给出
10
错误提示已经很明显了。这个root帐号登录不了啊。
10
连接字符串中 没有给出 root 的密码