標題: 想請教一下C#的問題 [列印本頁] 作者: kc100639 時間: 2011-5-30 18:49 標題: 想請教一下C#的問題 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace mysql
{
class Program
{
static void Main(string[] args)
{
Insert();
}
//
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.VarChar, 99);
parameters[1].Value = Ampere;
parameters[2] = new MySqlParameter("?volt", MySqlDbType.VarChar, 99);
parameters[2].Value = volt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
string data = string.Empty;
//開啟rs232 Port (開啟COM1,鮑率為19200,同為檢查為元為沒有,位元率為8,停止位元數為1)
SerialPort rs232 = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
string data = string.Empty;
//開啟rs232 Port (開啟COM1,鮑率為19200,同為檢查為元為沒有,位元率為8,停止位元數為1)
SerialPort rs232 = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
//開啟(建立)連線
rs232.Open();
while (true)
{
try
{
//由rs232讀入一個byte的資料
data = rs232.ReadLine();