c# - MySQL 到数组到 MSSQL 查询语句

标签 c# mysql arrays sql-server-2008

我正在开发一个程序来链接两个数据库(MySQL 和 MSSQL)并在数据网格表中显示它们。

我通过计数来获取数组值的数量,然后分配数组,然后使用数组值返回到数据网格表中。

我遇到的问题是skuArray[RowCount++] = Convert.ToInt32(myReader[0]);返回错误:无法将 int 转换为字符串。我把它改为skuArray[RowCount++] = Convert.String(myReader[0]);它正确编译,但给出消息 Object reference not set to an instance of an object.

所有 SQL 查询均已测试并成功执行。

代码:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
using System.Xml.Serialization;
using System.Data.SqlClient;

namespace SQL_Database_Connector
{
    public partial class Sync_Databases : Form
    {
        string serverInfo; // MySQL Database Information
        string portInfo;
        string databaseInfo;
        string usernameInfo;
        string passwordInfo;

        string MSserverInfo; // MSSQL Database Information
        string MSdatabaseInfo;
        string MSusernameInfo;
        string MSpasswordInfo;

        public string[] skuArray;
        public string queryString;
        public int RowCount;

        public Sync_Databases()
        {
            InitializeComponent();
        }

        private void Sync_Databases_Load(object sender, EventArgs e)
        {
            if (File.Exists("data.xml")) // MySQL Database
            {
                XmlSerializer xs = new XmlSerializer(typeof(Information));
                FileStream read = new FileStream("data.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
                Information info = (Information)xs.Deserialize(read);
                serverInfo = info.server;
                portInfo = info.port;
                databaseInfo = info.database;
                usernameInfo = info.username;
                passwordInfo = info.password;
                read.Close();
            }
            try
            {
                string MyConnection = String.Format("Server={0}; Port={1}; Database={2}; Uid={3}; Pwd={4};", serverInfo, portInfo, databaseInfo, usernameInfo, passwordInfo);
                string Query = "SELECT COUNT(*) " +
                               "FROM catalog_product_entity " +
                               "INNER JOIN catalog_product_entity_int " +
                               "ON catalog_product_entity_int.entity_id = catalog_product_entity.entity_id " +
                               "WHERE catalog_product_entity.sku IS NOT NULL " +
                               "AND catalog_product_entity.sku <> 0 " +
                               "AND(catalog_product_entity_int.attribute_id = '84' AND catalog_product_entity_int.value = '1');";
                MySqlConnection MyConn = new MySqlConnection(MyConnection);
                MySqlCommand MyCommand = new MySqlCommand(Query, MyConn);
                MyConn.Open();
                MySqlDataReader myReader;
                myReader = MyCommand.ExecuteReader();
                try
                {
                    while (myReader.Read())
                    {
                       RowCount = myReader.GetInt32(0); // Get Row Count
                       //MessageBox.Show(RowCount.ToString()); // Test Row Count
                    }
                }
                finally
                {
                    myReader.Close();
                    MyConn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem with Row Count: "+ ex.Message);
            }
            try
            {
                string MyConnection = String.Format("Server={0}; Port={1}; Database={2}; Uid={3}; Pwd={4};", serverInfo, portInfo, databaseInfo, usernameInfo, passwordInfo);
                string Query = "SELECT catalog_product_entity.sku AS 'SKU' " +
                               "FROM catalog_product_entity " +
                               "INNER JOIN catalog_product_entity_int " +
                               "ON catalog_product_entity_int.entity_id = catalog_product_entity.entity_id " +
                               "WHERE catalog_product_entity.sku IS NOT NULL " +
                               "AND catalog_product_entity.sku <> 0 " +
                               "AND(catalog_product_entity_int.attribute_id = '84' AND catalog_product_entity_int.value = '1');";
                MySqlConnection MyConn = new MySqlConnection(MyConnection);
                MySqlCommand MyCommand = new MySqlCommand(Query, MyConn);
                MyConn.Open();
                MySqlDataReader myReader;
                myReader = MyCommand.ExecuteReader();
                try
                {
                    while (myReader.Read())
                    {
                        skuArray[RowCount++] = Convert.ToString(myReader[0]); // Assigning Array Values
                        //MessageBox.Show(skuArray.ToString()); //T est
                    }
                }
                finally
                {
                    myReader.Close();
                    MyConn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem with MySQL query to capture Array: "+ ex.Message);
            }
            if (File.Exists("data2.xml")) // MSSQL Database
            {
                XmlSerializer xs = new XmlSerializer(typeof(Information));
                FileStream read = new FileStream("data2.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
                Information info = (Information)xs.Deserialize(read);
                MSserverInfo = info.server;
                MSdatabaseInfo = info.database;
                MSusernameInfo = info.username;
                MSpasswordInfo = info.password;
                read.Close();
            }
            try
            {
                string connectionString = string.Format("Data Source={0}; Initial Catalog={1}; User ID={2}; Password={3};", MSserverInfo, MSdatabaseInfo, MSusernameInfo, MSpasswordInfo);
                string sql = string.Format("SELECT ItemLookupCode,Description, Quantity, Price, LastReceived " +
                             "FROM Item " +
                             "WHERE ItemLookupCode IS IN {0} " +
                             "ORDER BY LastReceived ASC;", skuArray);
                SqlConnection connection = new SqlConnection(connectionString);
                SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
                DataSet ds = new DataSet();
                connection.Open();
                dataadapter.Fill(ds, "sql_table");
                connection.Close();
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = "sql_table";
                SqlConnection conn = new SqlConnection();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem with SQL query or connection: "+ ex.Message);
            }
        }
    }
}

最佳答案

你在哪里初始化了数组? 我没有看到任何一行会说:

skuArray = new string[RowCount];

RowCount 只是我正在使用的占位符。

关于c# - MySQL 到数组到 MSSQL 查询语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850480/

相关文章:

c# - ASP.NET 中的 javascript PageMethods - 传递参数

c# - 调整大小和移动具有透明度的图像控件

python - 将人类可读的日期转换为整数值

javascript - 在 JavaScript 中对多维数组使用过滤方法

c++ - 结构的动态数组

c# - 将表单添加到流布局面板

c# - 在 function.json 中参数化 runOnStartup

mysql - 根据条件加入 MySQL 表?

MYSQL:哪里不像 '%(SELECT column_name FROM table_name)%'

c - 逐行填充数组(一维数组)