C# MySql 在 Visual Studio 2012 中不起作用:找不到类型或命名空间名称“MySql”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19787270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySql doesn't work in Visual Studio 2012 : The type or namespace name 'MySql' could not be found
提问by JAN
Given this code :
鉴于此代码:
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.");
}
}
}
}
After compilation I get :
编译后我得到:
error CS0246: The type or namespace name 'MySqlConnection' could not be found (are you missing a using directive or an assembly reference?)
But I checked the connector :
但我检查了连接器:
And it's installed ..
它安装了..
So what's wrong ?
那么怎么了?
Thanks
谢谢
采纳答案by Damith
回答by ivan_pozdeev
Add a reference from the project to the MySql.Data.dll
as well.
将项目中的引用也添加到MySql.Data.dll
。
using
doesn't make assemblies available. It only allows you to use entries from a namespace without specifying that namespace each time.
using
不使程序集可用。它只允许您使用来自命名空间的条目,而无需每次都指定该命名空间。
回答by Arul Dinesh
We'll be adding support for VS 2012 in Connector/NET 6.5.5 and later 6.6.x version http://forums.mysql.com/read.php?38,546265,564533#msg-564533
我们将在 Connector/NET 6.5.5 和更高版本 6.6.x 版本中添加对 VS 2012 的支持http://forums.mysql.com/read.php?38,546265,564533#msg-564533
and give a link to test a trick
并给出一个测试技巧的链接
Refer this answer
参考这个答案
回答by broadband
You must add it as a reference
您必须将其添加为参考
回答by Nick
Just an idea,
只是一个想法,
Looks like you're missing MySql.Data in your project which you can declare in your web.config
看起来您的项目中缺少 MySql.Data,您可以在 web.config 中声明
Make sure you have your web.config properly configured.
确保正确配置了 web.config。
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<assemblyIdentity name="MySql.Web" publicKeyToken="c5687fc88969c44d" culture="neutral" />
回答by SAGAR KALBURGI
My solution:
我的解决方案:
Goto Project->Add reference->Extensions Here add MySql.Data and
转到 Project->Add reference->Extensions 这里添加 MySql.Data 和