using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace ConsoleApplication10
{
class Program
{
//程式片段大概是這樣
public int Insert(string Ohm, string Ampere, string volt)
{
//設定連線資訊
string connStr = String.Format("server=localhost;user id=root; password=123; database=test");
MySqlConnection conn = new MySqlConnection(connStr);
//sql字串
string sqlStr = "INSERT INTO tbl_SRBack (Ohm,Ampere,volt) VALUES (?Ohm,?Ampere,?volt)";
try
{
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
switch (ex.Number)
{
case 0:
Console.WriteLine("Can't Connect to test.");
break;
case 1045:
Console.WriteLine("Account or Password wrong.");
break;
}
return ex.Number;
}
MySqlCommand mySqlCmd = new MySqlCommand(sqlStr, conn);
//加入參數
MySqlParameter[] parameters = new MySqlParameter[3];
parameters[0] = new MySqlParameter("?Ohm", MySqlDbType.VarChar, 99);
parameters[0].Value = Ohm;
parameters[1] = new MySqlParameter("?Ampere", MySqlDbType.Int32);
parameters[1].Value = Ampere;
parameters[2] = new MySqlParameter("?volt", MySqlDbType.VarChar, 99);
parameters[2].Value = volt;