如何从DataProvider中得到相关信息来生成数据库无关的SQL Command
[ 2007-07-07 23:43:07 | 作者: progame ]
.net 2.0中machine.config中有dataprovider的注册信息, 比如说这样:
<connectionStrings>
<add name="TBPConnectionString" connectionString="Database=tbp;Data Source=localhost;User Id=root;Password=" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
machine.config中对应的provider configiration:
<system.data>
<DbProviderFactories>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.0.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
因为同样的功能, 在sql server中如果是这样的:
select [a], [c] from [t] where [id] = @id
那么在MySql中是这样的:
select `a`,`c` from `t` where `id` = ?id
那么如果我们只是根据web.config中的providerName来生成connection, 又该如何去生成native sql?
因为这个功能是我在AddIN中使用的, 所以我的sqlbroker只能把两个手拢袖筒里干瞪眼了:( 还好, 只是简单的CRUD, 不过就是有过滤, 否则我直接用commandtype = tabledirect再加上dataset就可以搞定了
先得到DbProviderFactory 吧:
DbProviderFactory factory = DbProviderFactories.GetFactory(providerName.Value);
然后要做些处理了:
DbDataAdapter adp = factory.CreateDataAdapter();
Connection.Open();
DbCommand cmd = factory.CreateCommand();
cmd.Connection = Connection;
DbCommandBuilder builder = factory.CreateCommandBuilder();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from table";
adp.SelectCommand = cmd;
builder.DataAdapter = adp;
prefix = builder.QuotePrefix;
suffix = builder.QuoteSuffix;
string temp = builder.GetInsertCommand(false).CommandText;
paramprefix = temp.Substring(temp.IndexOf("p1") - 1, 1);
Connection.Close();
OK, 这样一来, 起码object quotechar是得到了, parameter prefix也算勉强拿到了, 这样写出的SQL基本凑合了
评论Feed: /feed.asp?q=comment&id=883
<connectionStrings>
<add name="TBPConnectionString" connectionString="Database=tbp;Data Source=localhost;User Id=root;Password=" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
machine.config中对应的provider configiration:
<system.data>
<DbProviderFactories>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.0.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
因为同样的功能, 在sql server中如果是这样的:
select [a], [c] from [t] where [id] = @id
那么在MySql中是这样的:
select `a`,`c` from `t` where `id` = ?id
那么如果我们只是根据web.config中的providerName来生成connection, 又该如何去生成native sql?
因为这个功能是我在AddIN中使用的, 所以我的sqlbroker只能把两个手拢袖筒里干瞪眼了:( 还好, 只是简单的CRUD, 不过就是有过滤, 否则我直接用commandtype = tabledirect再加上dataset就可以搞定了
先得到DbProviderFactory 吧:
DbProviderFactory factory = DbProviderFactories.GetFactory(providerName.Value);
然后要做些处理了:
DbDataAdapter adp = factory.CreateDataAdapter();
Connection.Open();
DbCommand cmd = factory.CreateCommand();
cmd.Connection = Connection;
DbCommandBuilder builder = factory.CreateCommandBuilder();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from table";
adp.SelectCommand = cmd;
builder.DataAdapter = adp;
prefix = builder.QuotePrefix;
suffix = builder.QuoteSuffix;
string temp = builder.GetInsertCommand(false).CommandText;
paramprefix = temp.Substring(temp.IndexOf("p1") - 1, 1);
Connection.Close();
OK, 这样一来, 起码object quotechar是得到了, parameter prefix也算勉强拿到了, 这样写出的SQL基本凑合了
评论Feed: /feed.asp?q=comment&id=883
您可能感兴趣的文章:
这篇日志没有评论.

