-1

我想在使用之前从 dtSource 和 dtDestination 中检索一些值。但是,即使数据库中存储了一些值,我似乎也无法检索到其中的任何值。foreach 对检索某些值有用吗?我已经对其进行了调试,但它似乎在作为字符串传递之前没有检索 dtSource 和 dtDestination 中的值。

    static void Main(string[] args)
    {
        // Retrieve the connection string from Config table in master database
        string strConnFromConfig = getConfigDetails(SalesKey.DBConnection);

        DataTable dtSource = getSource(strConnFromConfig);
        DataTable dtDestination = getDestination(strConnFromConfig);
        DataTable dtRate = getRate(strConnFromConfig);
        DataTable dtCode = getCode(strConnFromConfig);

        //Having this issue here
        foreach (DataRow rwSrc in dtSource.Rows)
        {
            foreach (DataRow rwDest in dtDestination.Rows)
            {
                string src = rwSrc["Value"].ToString();
                string dest = rwDest["Value"].ToString();

                ExcelTemplate XLT = new ExcelTemplate();
                XLT.Open(src);
                DataBindingProperties dataProps = XLT.CreateDataBindingProperties();
                XLT.BindData(dtCode, "Code", dataProps);
                XLT.BindData(dtRate, "Rate", dataProps);

                XLT.Process();
                XLT.Save(dest);
            }
        }
    }

    // Retrieve the config details from master database
    static string getConfigDetails(SalesKey salesKey)
    {
        string strCon = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(strCon);

        if (sqlConnection.State == ConnectionState.Closed)
            sqlConnection.Open();

        string cSQL = "SELECT Value FROM dbo.COMMON_CONFIG WHERE Value = @value";

        SqlCommand sqlCommand = new SqlCommand(cSQL, sqlConnection);
        sqlCommand.Parameters.AddWithValue("@value", salesKey.ToString()); // Need to cast the enumeration value as string

        DataTable dataTable = new DataTable();
        dataTable.Load(sqlCommand.ExecuteReader());

        sqlCommand.Dispose();

        sqlConnection.Close();
        sqlConnection.Dispose();

        if (dataTable.Rows.Count > 0)
        {
            return dataTable.Rows[0]["Value"].ToString();
        }
        else
        {
            return string.Empty;
        }
    }

    // Generic method for sending in the SQL query to retrieve the specified dataset
    static DataTable getData(string connection, string query)
    {
        SqlConnection sqlConnection = new SqlConnection(connection);

        sqlConnection.Open();
        SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

        //SqlDataReader reader = sqlCommand.ExecuteReader();

        DataTable dataTable = new DataTable();
        dataTable.Load(sqlCommand.ExecuteReader());

        sqlCommand.Dispose();

        sqlConnection.Close();
        sqlConnection.Dispose();

        return dataTable;
    }

    static DataTable getSource(string connection)
    {
        connection = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
        string src = @"C:\Users\administrator.EBSDLAB\Desktop\Examples\Sales\currency.xlsx";
        string IdSQL = "SELECT VALUE FROM dbo.COMMON_CONFIG WHERE VALUE = '" + src + "'";
        return getData(connection, IdSQL);
    }

    static DataTable getDestination(string connection)
    {
        connection = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
        string dest = @"C:\Users\administrator.EBSDLAB\Desktop\Examples\Sales\output.xlsx";
        string IdSQL = "SELECT VALUE FROM dbo.COMMON_CONFIG WHERE VALUE = '" + dest + "'";
        return getData(connection, IdSQL);
    }
4

1 回答 1

1

您可能需要更改 SQL

"SELECT VALUE FROM dbo.COMMON_CONFIG WHERE VALUE = '" + src + "'";

您发送值并再次选择值!!!

我认为您可能需要通过Name以下方式获取值

"SELECT VALUE FROM dbo.COMMON_CONFIG WHERE Name= '" + src + "'";

相应地更改它,作为您的数据库列名,这适用于您的所有 SQL 字符串

注意:使用 SQL 参数和使用语句

于 2013-07-24T06:15:30.060 回答