现在的位置: 首页 > 综合 > 正文

MySQLDriverCS Exception: MySQLDriverCS Error: wrong query.No database selected

2012年12月07日 ⁄ 综合 ⁄ 共 3270字 ⁄ 字号 评论关闭

MySQLDriverCS Exception: MySQLDriverCS Error: wrong query.No database selected

AspNet连接mysql数据库

 

步骤:1.首先下载dll网址为:http://d.download.csdn.net/down/2625418/tailwind_cn

下下来之后又两个文件:libmySQL.dll和MySQLDriverCS.DLL

将他们引用到项目中来,但是第一个libmySql.dll没法引入,这时将libmySql.dll放到C:C:\WINDOWS\system32底下:

这样里面的文件就可以引用。

先把代码贴出来,方便大家建立测试:

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
       <div> </div>
        <asp:TextBox ID="tbName" runat="server"></asp:TextBox>
        <asp:TextBox ID="tbAddress" runat="server"></asp:TextBox>
        <asp:TextBox ID="tbTelephone" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="insert data" /><br />
    <div>
        <asp:GridView ID="gwShow" runat="server">
        </asp:GridView>
    </div>

    </form>
</body>
</html>
后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySQLDriverCS;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();

    }
    private void BindData()
    {
        string constr = ConfigurationManager.AppSettings["JICQMySql"];
        MySQLConnection conn = null;
        // conn = new MySQLConnection(new MySQLConnectionString("localhost", "protege_db", "root", "sdie?!3406").AsString);
        conn=new MySQLConnection(constr);
        //本地数据库 数据库名字 用户名root 密码
        conn.Open();
        MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
        commn.ExecuteNonQuery();       //设置set names gb2312   解决乱码
        string sql = "select * from animal";
        MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        mda.Fill(ds, "tb");
        this.gwShow.DataSource = ds.Tables["tb"];
        gwShow.DataBind();
        conn.Close();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MySQLConnection conn = null;
        conn = new MySQLConnection(new MySQLConnectionString("localhost", "protege_db", "root", "sdie?!3406").AsString);
        conn.Open();
        MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
        commn.ExecuteNonQuery();
        MySQLCommand comm = new MySQLCommand(string.Format("insert into animal (frame,frame_type,slot,facet,is_template,value_index,value_type) values ('{0}','{1}','{2}',1,1,1,1)", tbName.Text.Trim(), tbAddress.Text.Trim(), tbTelephone.Text.Trim()), conn);
        comm.ExecuteNonQuery();
        conn.Close();
        BindData();
    }

}
WebConfig中:

连接字符串为:

<appSettings>    <add key ="JICQMySql" value="Server=.; Data Source=protege_db;USER id=root;PASSWORD=sdie?!3406;" />  

</appSettings>

 

数据库,用户名和密码,查询的表大家根据自己的情况设定;

然后在浏览器中查看即可。

注意的是:在Web.Config 中的配置,有严格的限定,比如,Data Source不能改为:DataBase。User ID

不能改为:userID,Password不能改为:pwd等等,

 

标题中出的错误原因为:Data Source改为了DataBase,改回来即可。

MySQLDriverCS Exception: MySQLDriverCS Error: can't connect.Access denied for user 'ODBC'@'localhost' (using password: YES) 错误:

是因为:User ID 改为了uid ,

 

MySQLDriverCS Exception: MySQLDriverCS Error: can't connect.Access denied for user 'root'@'localhost' (using password: NO)

 

这个错误是因为:

password改为了pwd所致,改回来即可。

抱歉!评论已关闭.