【问题标题】:C# select SQL query with parameterC# 选择带参数的 SQL 查询
【发布时间】:2021-11-24 17:38:02
【问题描述】:

我想将数据库从 MS Access 迁移到 SQL Server,并且我正在尝试修改我的 SQL 查询。他们在 MS Access 中工作。

我有这个 C# 代码:

private DataSet GetIncident_ByIncident(string inc_num)
{
    MainIncident mi = new MainIncident();
    mi.incident_full_number = inc_num;

    string query = @"SELECT MainIncidentTable.Incident_Full_Num ,
            MainIncidentTable.Customer_Name ,
            MainIncidentTable.Service_Representative ,
            MainIncidentTable.Incident_Date ,
            MainIncidentTable.Average_Invoice ,
            MainIncidentTable.Street ,
            MainIncidentTable.City ,
            MainIncidentTable.Contact ,
            IncidentStatuses.Incident_Status_Name,
            Stations.Station_Name 

            FROM(MainIncidentTable INNER JOIN
                 Stations ON MainIncidentTable.Current_Station_ID = Stations.Station_ID)
                 INNER JOIN IncidentStatuses ON MainIncidentTable.Current_Incident_Status_Id = IncidentStatuses.Incident_Status_Id
            WHERE Incident_Full_Num = '@Incident_Full_Num'";

    List<OleDbParameter> l = new List<OleDbParameter>();

    l.Add(new OleDbParameter("@Incident_Full_Num", mi.incident_full_number));
    l.Add(new OleDbParameter("@Customer_Name", mi.customer_name));
    l.Add(new OleDbParameter("@Service_Representative", mi.service_representative));
    l.Add(new OleDbParameter("@Incident_Date", mi.incident_date));
    l.Add(new OleDbParameter("@Average_Invoice", mi.average_invoice));
    l.Add(new OleDbParameter("@Street", mi.street));
    l.Add(new OleDbParameter("@City", mi.city));
    l.Add(new OleDbParameter("@Contact", mi.contact));
    l.Add(new OleDbParameter("@Current_Station_ID", mi.current_station_id));
    l.Add(new OleDbParameter("@Current_Incident_Status_Id", mi.current_incident_status_id));

    var con = GetDataSetWithParameters(query, l);

    return con;
}

连接方式:

public static DataSet GetDataSetWithParameters(string query, List<OleDbParameter> parameters)
{
    DataSet ds = new DataSet();
       
    OleDbConnection Con = new OleDbConnection(sqlConnectionString);
    Con.Open();

    try
    {
        using (OleDbCommand cmd = new OleDbCommand(query, Con))
        {
            if (parameters != null)
            {
                cmd.Parameters.AddRange(parameters.ToArray());
            }

            using (OleDbDataAdapter Adapter = new OleDbDataAdapter(cmd))
            {
                Adapter.Fill(ds);
            }

            return ds;
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        CloseConnection(ref Con);
    }
}

SQL 查询正在运行,但未返回行。我猜这个问题是查询中参数的语法。

有人可以指导我吗?

【问题讨论】:

  • 这段代码会抛出异常吗?
  • 奇怪你有这么多未使用的参数。您应该使用SqlConnection 和SqlCommand 而不是Ole..。并使用using 块而不是try\finally

标签: c# sql sql-server query-parameters


【解决方案1】:

您不得将参数括在引号中。图书馆会处理这个问题。即使用

WHERE Incident_Full_Num = @Incident_Full_Num

而不是

WHERE Incident_Full_Num = '@Incident_Full_Num'

编辑

抱歉,忘记了,OleDb 不使用命名参数,而只使用位置参数。 See docs

所以要么将您的查询更新为

WHERE Incident_Full_Num = ?

或者您使用SqlCommand 和SqlParameter 而不是OleDbCommand 和OleDbParameter。

请注意,如果您使用OleDbCommand,则传递参数的顺序很重要。即,第一个? 将绑定到第一个参数,第二个? 将绑定到第二个参数,依此类推。如果您在查询中需要两次相同的参数,则还需要传递两次。

此外,似乎只使用了第一个参数。为什么要向命令传递大量未使用的参数?

【讨论】:

  • 以这种方式我得到异常:必须声明标量变量“@Incident_Full_Num”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多