【问题标题】:"Cannot implicitly convert type IDbConnection to SqlConnection"“无法将类型 IDbConnection 隐式转换为 SqlConnection”
【发布时间】:2014-06-13 15:32:18
【问题描述】:

我需要一些帮助。

我尝试在我的 SQL 数据库中创建一个INSERT,但我不能,因为在此代码行中给我一个错误:

 commandoSQL.Connection = dbcon;

我收到此错误:

Assets/NGUI/Scripts/Interaction/ChamarVariavel.cs(43,29):错误 CS0266:无法隐式转换类型 System.Data.IDbConnection' toSystem.Data.SqlClient.SqlConnection'。存在显式转换(您是否缺少演员表?)”

我希望有人可以帮助我。

谢谢

我的代码:

public class ChamarVariavel : MonoBehaviour {

    public UISlider slider;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
    }

    void OnGUI(){
        // Connection DB
        string connectionString = "Data Source=(local);Initial Catalog=Test;User ID=******;Password=*******";

        IDbConnection dbcon;

        dbcon= new SqlConnection(connectionString);

        dbcon.Open();
        //DB Online

        float x = slider.value * 100;
        GUI.Label(new Rect( 570, 238, 70, 30 ), "(" + x.ToString("f2") + ")");

        string qInsert = string.Format(@"INSERT INTO Fuel (fuel) VALUES ('{0}')", x);

        SqlCommand commandoSQL = new SqlCommand(qInsert);

        commandoSQL.Connection = dbcon;

        try
        {
            commandoSQL.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            GUI.Label(new Rect( 300, 40, 300, 300 ), ex.ToString());
        }

        dbcon.Close();
        //DB offline
    }
}

【问题讨论】:

  • 该错误与插入无关 - 它与Connection 属性的分配有关。关注相关问题。您应该能够摆脱强制转换 (commandoSQL.Connection = (SqlConnection)dbcon;),因为它是作为 new SqlConnection(connectionString) 创建的,尽管使用更好的类型变量(varSqlConnection)可以完全避免这种情况。
  • @user2864740 您在评论中几乎回答了他的问题。如果你真的回答了它,那么它可能会给你加分,但也让人们更容易看到正确的答案,代码格式看起来更好等等。但是你是对的 ;-)
  • 我建议使用 IDbConnection 中的 CreateComman 方法。 msdn.microsoft.com/en-us/library/…

标签: c# sql sql-server unity3d


【解决方案1】:

好吧,这会起作用:

commandoSQL.Connection = (SQLConnection)dbcon;

因为 dbcon 被声明为 IDBConnection 但实际上是 SQL 连接,但这有点愚蠢。

简单地做

var dbcon = new SqlConnection(connectionString);

并删除

IDbConnection dbcon;

并且它将被声明为正确的类型。我没有理由看到它为什么需要声明为 IDBconnection。

【讨论】:

  • 使用 'var' 确实是声明 dbcon 的首选方式,因为编译器总是会选择正确的类型(var 不像 VB6 中的旧 Variant 类型)。如果由于某种原因底层 SqlConnection 的类型应该更改或重命名(可能发生),您的代码仍然可以编译/工作。
【解决方案2】:

那里有另一种方法,它有助于概括您的数据访问代码:让连接创建命令:

using(var commandoSQL = dbcon.CreateCommand()) {
    commandoSQL.CommandText = SQL;
    //..
}

在其他新闻中:使用参数,而不是 string.Format。 SQL 注入是个大问题。

【讨论】:

    【解决方案3】:

    该错误实际上是在告诉您问题所在,您需要显式转换该对象,因为SqlCommand 正在寻找SqlConnection,请考虑这样做:

    new SqlCommand(qInsert, (SqlConnection)dbcon);
    

    并删除这一行:

    commandoSQL.Connection = dbcon;
    

    另一种选择是将dbcon 定义为SqlConnection

    SqlConnection dbcon
    

    然后你可以这样做:

    new SqlCommand(qInsert, dbcon);
    

    最后,看看我不久前写的blog post;您需要更改使用对象的方式。

    【讨论】:

    • 谢谢,完美解决,解决了。顺便恭喜博客很好,我喜欢它。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2022-01-15
    • 2016-07-31
    相关资源
    最近更新 更多