DataTableMapping使用适用于存储过程的adapter.FillSchema方法

时间:2021-12-17 16:27:49

this is a method i was reading about @MSDN , my question is if for an example i would like to use it on a stored procedure with the fact that the query of that stored procedure is already specifying columns to select from the table like following :

这是我正在阅读的有关@MSDN的方法,我的问题是,如果我想在存储过程中使用它,并且该存储过程的查询已经指定要从表中选择的列,如下所示:

SELECT Columnsome, columnother, , , , ...FROM thisSQLdbTable

though i would like to implement the approach of that specific method , it seems very advanced from a little research i have made on "the best way" available to extract data from SQL Server into Asp.net DataTable.

虽然我想实现这种特定方法的方法,但是我对从SQL Server中提取数据到Asp.net DataTable的“最佳方法”的一些研究似乎非常先进。

public static DataTable GetCustomerData(string dataSetName,
string connectionString)
{
DataTable table = new DataTable(dataSetName);

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlDataAdapter adapter = new SqlDataAdapter(
        "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", connection);

    DataTableMapping mapping = adapter.TableMappings.Add("Table", "Customers");
    mapping.ColumnMappings.Add("CompanyName", "Name");
    mapping.ColumnMappings.Add("ContactName", "Contact");

    connection.Open();

    adapter.FillSchema(table, SchemaType.Mapped);
    adapter.Fill(table);
    return table;
}

}

}

or is it not the method to use if i am querying via SP that specifies the selected column

或者,如果我通过SP查询指定所选列,则不使用该方法

i could actually drop that stored procedure if it is not requiered to select /specify columns

如果不需要选择/指定列,我实际上可以删除该存储过程

the stored procedure is doing a specific calculation and updates the table with results of calculation then i am switching it's "MODE" to select results from the table that was updated.

存储过程正在进行特定的计算并使用计算结果更新表,然后我将其“MODE”切换为从已更新的表中选择结果。

what i did is recyceling (; giving it a parameter (bit type) stored procedure then asks for the value of supplied bool / bit Parameter, if its is status true it updates (doing its original task it was made for)

我所做的是recyceling(;给它一个参数(位类型)存储过程然后请求提供的bool / bit参数的值,如果它的状态为true则更新(执行它的原始任务)

if its false its doing a select oporation so i am using it as i would with 2 separated commands

如果它是假的它做一个选择oporation所以我使用它,因为我会用2个单独的命令

but now that i have search for a better way to extract data from db into a Data table

但现在我已经在寻找一种更好的方法将数据从db提取到Data表中

i give up on the two way SP and i will make a selection via the exaple above if they're not intended to be used thogether as with my current SP that does preselection when servs the GetCustomersData() above.

我放弃了双向SP,我将通过上面的exaple进行选择,如果它们不打算像我当前的SP那样在上面的GetCustomersData()服务时进行预选。

So the question is do i need to let the function to make the selection or can i serve it with my sp ready made selection to implemet it with GetCustomersData() in the way that it will only do rest of task and only mapp the columns that was preselected

所以问题是我是否需要让函数进行选择,或者我可以使用我的sp现成选择来实现它以使用GetCustomersData()实现它只会执行其余任务的方式并且只对mapp的列进行mapp被预选

2 个解决方案

#1


1  

Still a bit confused on your actual requirement but here goes:

仍然有点困惑你的实际要求,但在这里:

I See you are using a direct query in your C# code, 'best way' would be to make a SP out of it then say:

我看到你在C#代码中使用了直接查询,“最好的方法”就是用它制作一个SP,然后说:

SqlCommand command = conn.CreateCommand();
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "sp_GetCustomerData";

Then after you have added parameters if needed do:

然后,如果需要,添加参数后:

            conn.Open();
            sqlAdapter.Fill(dtResult);                
            conn.Close();

Where dtResult is Datatable. So you do not need to do any mapping in this case, and since you are using a SP from the Database it will work faster than your direct query and you can change the query logic any time without the need of re deploying your code.

其中dtResult是Datatable。因此,在这种情况下您不需要执行任何映射,并且由于您使用的是数据库中的SP,它将比直接查询更快地工作,您可以随时更改查询逻辑,而无需重新部署代码。

#2


1  

Stored procedures are perfectly valid in this use case. however, if you want more of a properly mapped table, you have several options, some of which go beyond the use of DataTables.

存储过程在此用例中完全有效。但是,如果你想要更多正确映射的表,你有几个选项,其中一些选项超出了DataTables的使用范围。

You can use strongly typed DataSets or perhaps use an ORM (object relational mapper).

您可以使用强类型DataSet或使用ORM(对象关系映射器)。

ref: Typed Datasets: http://msdn.microsoft.com/en-us/library/esbykkzb(v=vs.71).aspx

ref:类型化数据集:http://msdn.microsoft.com/en-us/library/esbykkzb(v = vs.71).aspx

ref: What is an ORM : http://en.wikipedia.org/wiki/Object-relational_mapping

参考:什么是ORM:http://en.wikipedia.org/wiki/Object-relational_mapping

EXAMPLES OF ORM'S

ORM的例子

ref: Entity Framework : http://msdn.microsoft.com/en-us/data/ef.aspx

ref:Entity Framework:http://msdn.microsoft.com/en-us/data/ef.aspx

ref: NHibernate: http://nhforge.org/

ref:NHibernate:http://nhforge.org/

#1


1  

Still a bit confused on your actual requirement but here goes:

仍然有点困惑你的实际要求,但在这里:

I See you are using a direct query in your C# code, 'best way' would be to make a SP out of it then say:

我看到你在C#代码中使用了直接查询,“最好的方法”就是用它制作一个SP,然后说:

SqlCommand command = conn.CreateCommand();
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "sp_GetCustomerData";

Then after you have added parameters if needed do:

然后,如果需要,添加参数后:

            conn.Open();
            sqlAdapter.Fill(dtResult);                
            conn.Close();

Where dtResult is Datatable. So you do not need to do any mapping in this case, and since you are using a SP from the Database it will work faster than your direct query and you can change the query logic any time without the need of re deploying your code.

其中dtResult是Datatable。因此,在这种情况下您不需要执行任何映射,并且由于您使用的是数据库中的SP,它将比直接查询更快地工作,您可以随时更改查询逻辑,而无需重新部署代码。

#2


1  

Stored procedures are perfectly valid in this use case. however, if you want more of a properly mapped table, you have several options, some of which go beyond the use of DataTables.

存储过程在此用例中完全有效。但是,如果你想要更多正确映射的表,你有几个选项,其中一些选项超出了DataTables的使用范围。

You can use strongly typed DataSets or perhaps use an ORM (object relational mapper).

您可以使用强类型DataSet或使用ORM(对象关系映射器)。

ref: Typed Datasets: http://msdn.microsoft.com/en-us/library/esbykkzb(v=vs.71).aspx

ref:类型化数据集:http://msdn.microsoft.com/en-us/library/esbykkzb(v = vs.71).aspx

ref: What is an ORM : http://en.wikipedia.org/wiki/Object-relational_mapping

参考:什么是ORM:http://en.wikipedia.org/wiki/Object-relational_mapping

EXAMPLES OF ORM'S

ORM的例子

ref: Entity Framework : http://msdn.microsoft.com/en-us/data/ef.aspx

ref:Entity Framework:http://msdn.microsoft.com/en-us/data/ef.aspx

ref: NHibernate: http://nhforge.org/

ref:NHibernate:http://nhforge.org/