试图从数据库中首先获取Mysql代码

时间:2015-04-29 16:22:05

标签: c# mysql visual-studio-2013

编辑:发现这篇文章并且有效,但必须从Win表单运行:

Enable Entity Framework 6 for MySql (C#) in WinForms of Microsoft Visual Studio 2013

OP --------------------

我创建了一个控制台C#应用程序

  • 当我右键点击我的项目时,
  • 转到添加新项目,
  • 我添加了名为Model1
  • 的ADO.Net实体数据模型
  • 然后我选择MySQL连接并点击完成

什么都没发生。它只是关闭向导而什么都不做。

有人遇到过这个问题吗?

我已经安装了用于Visaul Studio程序的MySQL,并且还运行了这个NPM命令:

Install-Package Mysql.Data.Entities

我的App.config看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"></remove>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0" />
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.8.5.0" newVersion="6.8.5.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

1 个答案:

答案 0 :(得分:0)

需要连接字符串才能连接到数据库。那里你没有那些信息。

假设您已经安装了 Mysql.Data ,请尝试以下代码:

    MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "yourserver.com";
conn_string.UserID = "youruser";
conn_string.Password = "yourpass";
conn_string.Database = "yourdatabasename";

using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
using (MySqlCommand cmd = conn.CreateCommand())
{    //watch out for this SQL injection vulnerability below
     cmd.CommandText = string.Format("SELECT * FROM Users --Just to test");
     connection.Open();
     cmd.ExecuteNonQuery();
}