c# - MySQL to Array to MSSQL Query Statement

时间:2016-11-28 17:48:41

标签: c# mysql arrays sql-server-2008

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

我得到一个计数来获取数组的值,然后分配数组,然后使用数组值返回到datagrid表。

我遇到的问题是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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你在哪里初始化阵列? 我看不到任何说法的界限 skuArray = new string [RowCount]; RowCount只是我正在使用的占位符。