MySql 在 Visual Studio 2012 中不起作用:找不到类型或命名空间名称“MySql”
Posted
技术标签:
【中文标题】MySql 在 Visual Studio 2012 中不起作用:找不到类型或命名空间名称“MySql”【英文标题】:MySql doesn't work in Visual Studio 2012 : The type or namespace name 'MySql' could not be found 【发布时间】:2013-11-16 05:24:45 【问题描述】:鉴于此代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//Include mysql client namespace.
using MySql.Data.MySqlClient; // That one doesn't work !!!
using System.Configuration;
namespace CSharpMySqlSample
public partial class frmMySqlSample : Form
//Read connection string from application settings file
string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
MySqlConnection connection;
MySqlDataAdapter adapter;
DataTable DTItems;
public frmMySqlSample()
InitializeComponent();
private void frmMySqlSample_Load(object sender, EventArgs e)
//Initialize mysql connection
connection = new MySqlConnection(ConnectionString);
//Get all items in datatable
DTItems = GetAllItems();
//Fill grid with items
dataGridView1.DataSource = DTItems;
//Get all items from database into datatable
DataTable GetAllItems()
try
//prepare query to get all records from items table
string query = "select * from items";
//prepare adapter to run query
adapter = new MySqlDataAdapter(query, connection);
DataSet DS = new DataSet();
//get query results in dataset
adapter.Fill(DS);
// Set the UPDATE command and parameters.
adapter.UpdateCommand = new MySqlCommand(
"UPDATE items SET ItemName=@ItemName, Price=@Price, AvailableQuantity=@AvailableQuantity, Updated_Dt=NOW() WHERE ItemNumber=@ItemNumber;",
connection);
adapter.UpdateCommand.Parameters.Add("@ItemNumber", MySqlDbType.Int16, 4, "ItemNumber");
adapter.UpdateCommand.Parameters.Add("@ItemName", MySqlDbType.VarChar, 100, "ItemName");
adapter.UpdateCommand.Parameters.Add("@Price", MySqlDbType.Decimal, 10, "Price");
adapter.UpdateCommand.Parameters.Add("@AvailableQuantity", MySqlDbType.Int16, 11, "AvailableQuantity");
adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
// Set the INSERT command and parameter.
adapter.InsertCommand = new MySqlCommand(
"INSERT INTO items VALUES (@ItemNumber,@ItemName,@Price,@AvailableQuantity,NOW());",
connection);
adapter.InsertCommand.Parameters.Add("@ItemNumber", MySqlDbType.Int16, 4, "ItemNumber");
adapter.InsertCommand.Parameters.Add("@ItemName", MySqlDbType.VarChar, 100, "ItemName");
adapter.InsertCommand.Parameters.Add("@Price", MySqlDbType.Decimal, 10, "Price");
adapter.InsertCommand.Parameters.Add("@AvailableQuantity", MySqlDbType.Int16, 11, "AvailableQuantity");
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
// Set the DELETE command and parameter.
adapter.DeleteCommand = new MySqlCommand(
"DELETE FROM items "
+ "WHERE ItemNumber=@ItemNumber;", connection);
adapter.DeleteCommand.Parameters.Add("@ItemNumber",
MySqlDbType.Int16, 4, "ItemNumber");
adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
//return datatable with all records
return DS.Tables[0];
catch (Exception ex)
MessageBox.Show(ex.Message);
return null;
private void btnSave_Click(object sender, EventArgs e)
try
//Save records in database using DTItems which is datasource for Grid
adapter.Update(DTItems);
//Refresh grid
DTItems = GetAllItems();
dataGridView1.DataSource = DTItems;
MessageBox.Show("Items saved successfully...");
catch(Exception ex)
MessageBox.Show(ex.Message);
private void btnDelete_Click(object sender, EventArgs e)
if (dataGridView1.SelectedRows.Count > 0)
//Delete a row from grid first.
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
//Save records again. This will delete record from database.
adapter.Update(DTItems);
//Refresh grid. Get items again from database and show it in grid.
DTItems = GetAllItems();
dataGridView1.DataSource = DTItems;
MessageBox.Show("Selected item deleted successfully...");
else
MessageBox.Show("You must select entire row in order to delete it.");
编译后我得到:
error CS0246: The type or namespace name 'MySqlConnection' could not be found (are you missing a using directive or an assembly reference?)
但我检查了连接器:
它已经安装了..
那怎么了?
谢谢
【问题讨论】:
【参考方案1】:您需要添加对MySql.Data.dll
的引用,但最好可以使用NuGet 将此dll
添加为包
下面是包管理器控制台命令
PM> Install-Package MySql.Data
【讨论】:
非常感谢,这是唯一可以在我的 Ubuntu 上运行的东西 安装后从添加参考面板下的浏览中添加这个 @CharithaGoonewardena 不需要这样做。【参考方案2】:也将项目中的引用添加到MySql.Data.dll
。
using
不提供程序集。它只允许您使用命名空间中的条目,而无需每次都指定该命名空间。
【讨论】:
【参考方案3】:您必须将其添加为参考
【讨论】:
【参考方案4】:我们将在 Connector/NET 6.5.5 和更高版本的 6.6.x 版本 http://forums.mysql.com/read.php?38,546265,564533#msg-564533 中添加对 VS 2012 的支持
并给出一个测试技巧的链接
http://social.technet.microsoft.com/wiki/pt-br/contents/articles/10476.instalando-mysql-connector-no-visual-studio-2011-beta.aspx
参考这个answer
【讨论】:
【参考方案5】:只是一个想法,
您的项目中似乎缺少 MySql.Data,您可以在 web.config 中声明它
确保您的 web.config 配置正确。
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<assemblyIdentity name="MySql.Web" publicKeyToken="c5687fc88969c44d" culture="neutral" />
【讨论】:
请注意,这不是一个 Web 项目,因此不会有web.config
文件,但很好的建议以供将来参考。【参考方案6】:
我的解决方案:
转到项目->添加参考->扩展 这里添加 MySql.Data 和
【讨论】:
以上是关于MySql 在 Visual Studio 2012 中不起作用:找不到类型或命名空间名称“MySql”的主要内容,如果未能解决你的问题,请参考以下文章
在 Visual Studio 2012 / 2013 中使用 SSIS BIDS
Visual Studio2015 Nuget安装OpencvSharp包