使用C#将字符串数组转换为mysql [英] String array to mysql using C#

查看:198
本文介绍了使用C#将字符串数组转换为mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的SQL中添加总共11条记录。由4列组成,总共44个块。



所以我所拥有的是

/ br / >
名称,IP地址,端口,标记



我只是想在名称列中加载测试,我不断得到连接必须有效并打开



我已经测试了MySQL连接字符串和查询只是通过发送一个管道并且它工作,当我尝试使用for循环时发送给他们一个接一个



以下是我的按下功能的表格



我尝试过的方法:



I want to add a total of 11 records in my SQL. the consist of 4 columns, so a total of 44 blocks.

so what I have is information in the form of

Name,IPAdress,Port,Tag

I am just trying to load in the Name column for a test and I keep getting "connection must be valid and open"

I have tested the MySQL connection string and the Query just by sending one down the pipe and it works, its when I try to use a for loop to send them one after another

below is my "on press" function of the forms

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
   Globals.camera1Name = cameraName1.Text; Globals.camera1IP = cameraIP1.Text;  
   Globals.camera1Port = cameraPort1.Text;
   Globals.camera2Name = cameraName2.Text; Globals.camera2IP = cameraIP2.Text;  
   Globals.camera2Port = cameraPort2.Text;
   Globals.camera3Name = cameraName3.Text; Globals.camera3IP = cameraIP3.Text;  
   Globals.camera3Port = cameraPort3.Text;
   Globals.camera4Name = cameraName4.Text; Globals.camera4IP = cameraIP4.Text; 
   Globals.camera4Port = cameraPort4.Text;

   Globals.userName = newUserName.Text; Globals.userPassword = newUserPassword.Text;

   Globals.wonderWareIP = wonderWareIP.Text; 
   Globals.wonderWarePort = wonderWarePort.Text; 
   Globals.wonderWareTag = wonderWareTag.Text;       

   //string command = "UPDATE `james_hardie`.`config` SET `system_name`='" +  Globals.printer1Name + "', `ip_address`='" + Globals.printer1IP + "', `port`='" + Globals.printer1print + "', `cartridges`='" + Globals.printer1Cart + "' WHERE `idconfig`='1';";

   //MySqlCommand excute = new MySqlCommand(command, Globals.hardie);
   MySqlDataReader myReader;

   try
   {
      Globals.hardie.Open();

      for (int x = 0; x < 11; x++)
      {
         string command = "UPDATE `james_hardie`.`config` SET `system_name`='" + Globals.printerNames[x] + "' WHERE `idconfig`='" + (x + 1) + "';";
         MySqlCommand excute = new MySqlCommand(command, Globals.hardie);
         myReader = excute.ExecuteReader();
         Globals.hardie.Close();
      }
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}
}

推荐答案

您正在关闭循环内部的连接。它将是第一次,但下次数据库连接未打开,因此您将获得异常。其次使用 ExecuteNonQuery()而不是 ExecuteReader()。尝试使用以下代码:

You are closing the connection inside loop. It will for first time but next time Database connection is not opened so you are getting exception. Secondly use ExecuteNonQuery() instead of ExecuteReader(). Try with below code:
try
{
  // Open connection
  Globals.hardie.Open();

  for (int x = 0; x < 11; x++)
  {
	 string command = "UPDATE `james_hardie`.`config` SET `system_name`='" + Globals.printerNames[x] + "' WHERE `idconfig`='" + (x + 1) + "';";
	 MySqlCommand excute = new MySqlCommand(command, Globals.hardie);
	 excute.ExecuteNonQuery();
  }
  
  // Close connection
  Globals.hardie.Close();
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message);
}



虽然上面一个解决了你的问题仍然有 SQL注入问题,所以总是使用如下的参数化查询:


Although above one resolve your problem stills it has SQL Injection problem, so always use parameterised query like below:

MySqlCommand cmd1; 
cmd1 = new MySqlCommand("SELECT * FROM User_table WHERE User_ID = @UserID", Globals.hardie);
cmd1.Parameters.AddWithValue("@UserID", "yourValue"));
excute.ExecuteNonQuery();
cmd1.Parameters.Clear();


这篇关于使用C#将字符串数组转换为mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆