如何将 select 语句中的记录与 c# 中的字符串进行匹配?
How would you match a record from a select statement against a string in c#?
我正在尝试创建一个语句来根据 c#
中的字符串检查来自 foxpro 数据库的数据
但是我似乎无法让它工作,在这里使用参数化查询是否有助于实现我想要做的事情?
string PROPCODE = "IMPORT_" + ID;
string leadtenant = clcodet;
using (OleDbCommand tenantpopulation = new OleDbCommand(@"SELECT
CLCODE,
CLCODEDESC
FROM CLIENT WHERE PROPCODET = " + PROPCODE, importConnection))
{
string tenants = "";
if (@"CLCODE" = leadtenant)
{
if (tenants != String.Empty)
{
//do something
}
}
}
澄清一下,我想检查从 tenantpopulation 调用的 CLCODE 是否与代码中其他地方定义的 leadtenant 匹配
正如其他人已经指出的那样,使用参数是可行的方法(不仅在 VFP 中,而且在任何 SQL 数据库中)。它们不仅用于防止 SQL 注入攻击,还使用驱动程序负责将参数转换为正确的字符串、adding/removing 大括号、引号等
string PROPCODE = "IMPORT_" + ID;
string leadtenant = clcodet;
using (OleDbCommand tenantpopulation = new OleDbCommand(@"SELECT
CLCODE
FROM CLIENT WHERE PROPCODET = ?", importConnection))
{
tenantpopulation.Parameters.AddWithValue("p", PROPCODE);
// rest of code seem to be meaningless
// and I didn't see any code where you run your query
// depending on your requirement, I assume PROPCODET is a primary key?
// if so then you to do the check you only need to return the CLCODE
// with ExecuteScalar:
importConnection.Open();
var clcode = (string)tenantpopulation.ExecuteScalar();
importConnection.Close();
string tenants = "";
// in C# equality check is done with == NOT = (assingment)
if (clcode == leadtenant)
{
// tenants is always String.Empty here
if (tenants != String.Empty)
{
//do something
}
}
}
PS:你有没有想过,使用来自codeplex的Tom Brother的LinqToVFP?使用 Linq,您不需要了解这些 SQL 方言,而是使用对象查询(和智能感知)。
我正在尝试创建一个语句来根据 c#
中的字符串检查来自 foxpro 数据库的数据但是我似乎无法让它工作,在这里使用参数化查询是否有助于实现我想要做的事情?
string PROPCODE = "IMPORT_" + ID;
string leadtenant = clcodet;
using (OleDbCommand tenantpopulation = new OleDbCommand(@"SELECT
CLCODE,
CLCODEDESC
FROM CLIENT WHERE PROPCODET = " + PROPCODE, importConnection))
{
string tenants = "";
if (@"CLCODE" = leadtenant)
{
if (tenants != String.Empty)
{
//do something
}
}
}
澄清一下,我想检查从 tenantpopulation 调用的 CLCODE 是否与代码中其他地方定义的 leadtenant 匹配
正如其他人已经指出的那样,使用参数是可行的方法(不仅在 VFP 中,而且在任何 SQL 数据库中)。它们不仅用于防止 SQL 注入攻击,还使用驱动程序负责将参数转换为正确的字符串、adding/removing 大括号、引号等
string PROPCODE = "IMPORT_" + ID;
string leadtenant = clcodet;
using (OleDbCommand tenantpopulation = new OleDbCommand(@"SELECT
CLCODE
FROM CLIENT WHERE PROPCODET = ?", importConnection))
{
tenantpopulation.Parameters.AddWithValue("p", PROPCODE);
// rest of code seem to be meaningless
// and I didn't see any code where you run your query
// depending on your requirement, I assume PROPCODET is a primary key?
// if so then you to do the check you only need to return the CLCODE
// with ExecuteScalar:
importConnection.Open();
var clcode = (string)tenantpopulation.ExecuteScalar();
importConnection.Close();
string tenants = "";
// in C# equality check is done with == NOT = (assingment)
if (clcode == leadtenant)
{
// tenants is always String.Empty here
if (tenants != String.Empty)
{
//do something
}
}
}
PS:你有没有想过,使用来自codeplex的Tom Brother的LinqToVFP?使用 Linq,您不需要了解这些 SQL 方言,而是使用对象查询(和智能感知)。