简体   繁体   English

C#.NET sqlDatabase CommandBuilder 和 DataAdapter:名称“适配器”在当前上下文中不存在

[英]C#.NET sqlDatabase CommandBuilder and DataAdapter: The name 'Adapter' does not exit in the current context

I've been following a C#.Net tutorial online.我一直在关注 C#.Net 在线教程。 Created a class and have declare the variable Adapter1 for my DataAdapter.创建了一个class并为我的 DataAdapter 声明了变量Adapter1

I'm using CommandBuilder with Adapter1 to update, save, delete or insert new record to the database.我正在使用带有Adapter1的 CommandBuilder 来更新、保存、删除或将新记录插入数据库。

The problem I'm having is that the UpdateDatabase method I've declared seems not to see the variable Adapter1 .我遇到的问题是我声明的UpdateDatabase方法似乎看不到变量Adapter1

Please take a look at the code below and tell me what I'm doing wrong.请看看下面的代码,告诉我我做错了什么。 Code that's causing error is at the very bottom导致错误的代码位于最底部

private string sql_string;
private string strCon; //This is a write-only property

public string Sql
{
    set { sql_string = value; }
}

public string connection_string
{
    set { strCon = value; }
}

public System.Data.DataSet GetConnection
{
    get { return MyDataSet(); }
}

private System.Data.DataSet MyDataSet()
{
    System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
    con.Open();

    System.Data.SqlClient.SqlDataAdapter Adapter1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);

    System.Data.DataSet dat_set = new System.Data.DataSet();
    Adapter1.Fill(dat_set, "Table_Data_1");

    con.Close();

    return dat_set;
}

public void UpdateDatabase (System.Data.DataSet ds)
{
    System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(Adapter1);
    cb.DataAdapter.Update(ds.Tables[0]);
}

The reason you're getting this specific error is that there is no variable named Adapter1 that is in scope in function UpdateDatabase() .您收到此特定错误的原因是 function UpdateDatabase()的 scope 中没有名为Adapter1的变量。 You could make the local variable that you've declared in function MyDataSet() a member variable, then it would be visible in UpdateDatabase() .您可以将您在 function MyDataSet()中声明的局部变量设为成员变量,然后它将在UpdateDatabase()中可见。 This is not the best approach, however, because you'll be counting on code that uses this helper class to always call the MyDataSet() function before the UpdateDatabase() function, otherwise the data adapter won't be initialized (you'll get a null reference error).但是,这不是最好的方法,因为您将指望使用此帮助程序 class 的代码始终在UpdateDatabase() ZC1C425268E68385D1AB5074C1 之前调用MyDataSet() function ZC1C425268E68385D1AB5074C1得到 null 参考错误)。 If you want to structure the code this way, you should initialize your adapter in the constructor of the class that you're building, that way the code that uses your library can be sure that the adapter will always be initialized properly.如果您想以这种方式构造代码,您应该在您正在构建的 class 的构造函数中初始化您的适配器,这样使用您的库的代码可以确保适配器将始终正确初始化。 If you structure the code that way, you can make the connection string a constructor parameter instead of a write- only property.如果以这种方式构建代码,则可以将连接字符串作为构造函数参数,而不是只写属性。

By all means keep working on this code as an exercise, but be aware that DataAdapters and DataSets are a very old part of .NET, and this idiom isn't used much in modern data tiers.一定要继续编写此代码作为练习,但请注意,DataAdapters 和 DataSets 是 .NET 的一个非常古老的部分,并且这个习语在现代数据层中使用得不多。 Either people use ORMs (like EntityFramework) or they use ADO.NET commands directly if they want control and efficiency.人们要么使用 ORM(如 EntityFramework),要么直接使用 ADO.NET 命令,如果他们想要控制和效率。 I would also point out that the class you're building appears to be trying to fill the role of a DataAdapter, so it wouldn't have any utility in real life (people would just use a SqlDataAdapter directly instead of your class).我还要指出,您正在构建的 class 似乎正在尝试扮演 DataAdapter 的角色,因此它在现实生活中没有任何实用性(人们只会直接使用SqlDataAdapter而不是您的类)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM