- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的目标是将数据从 C# 发送到 SQL 服务器,但数据库端什么也没有发生。可能是什么问题?也许它在数据库端的数据类型错误?
获取字符串并发送的代码。
完整代码
namespace NIBP2PC
{
public partial class Form1 : Form
{
private delegate void displayDeleg(string message);
const string STX = "\u0002"; //Start
const string ETX = "\u0003"; //End
const string STARTMEAS = "01;;D7"; //Command Values
const string STOPMEAS = "X";
const string SETCYCLE0 = "03;;D9"; // Manual mode
const string SETCYCLE1 = "04;;DA"; // 1 min
const string SETCYCLE2 = "05;;DB"; // 2 min
const string SETCYCLE3 = "06;;DC"; // 3 min
const string SETCYCLE4 = "07;;DD"; // 4 min
const string SETCYCLE5 = "08;;DE"; // 5 min
const string SETCYCLE10 = "09;;DF"; // 10 min
const string SETCYCLE15 = "10;;D7"; // 15 min
const string SETCYCLE30 = "11;;D8"; // 30 min
const string SETMANO = "14;;DB"; // Manometer mode
const string SETREBOOT = "15;;DC"; // Reset Board
const string SETLEAK = "17;;DE"; // Leakage Test
const string READSTATUS = "18;;DF"; // Read Result
const string SETPMP100 = "19;;E0"; // Set start pressure 100mmHg
const string SETPMP120 = "20;;D8"; // Set start pressure 120mmHg
const string SETPMP140 = "21;;D9"; // Set start pressure 140mmHg
const string SETPMP160 = "22;;DA"; // Set start pressure 160mmHg
const string SETPMP180 = "23;;DB"; // Set start pressure 180mmHg
const string SETADULT = "24;;DC"; // Set Adult Mode
const string SETNEO = "25;;DD"; // Set Neo Mode
const byte INIT = 0; //Not measured up to now
const byte OK = 1; // Status Values
const byte RSTAT = 2; // Read Status
const byte RPRESS = 3;
byte V_Cycle;
byte V_Pumpup;
int V_Map;
// private VerticalProgressBar bar1 = new VerticalProgressBar();
public Form1()
{
InitializeComponent();
list_comport();
}
private void list_comport()
{
// Get a list of serial port names.
string[] ports = SerialPort.GetPortNames();
// Display each port name to the console.
foreach (string port in ports)
{
portToolStripMenuItem.DropDownItems.Add(port, null, new EventHandler(port_Click));
}
}
private void port_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
serialPort1.Close();
serialPort1.ReadBufferSize = 64;
serialPort1.ReceivedBytesThreshold = 2;
string Port = sender.ToString();
serialPort1.PortName = Port;
try
{
serialPort1.Open();
}
catch
{
MessageBox.Show("Serial port " + serialPort1.PortName +
" cannot be opened!", "RS232 tester",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
};
toolStripStatusLabel_Com.Text = Port;
Send_command(SETCYCLE0);
Send_command(SETADULT);
label_Status.Text = "IDLE";
label_Cycle.Text = "Manual";
label_Patient.Text = "Adult";
label_Pump.Text = "160 mmHg";
V_Cycle = 0;
V_Pumpup = 3;
Send_command(SETPMP160);
Send_command(READSTATUS); // Read status values
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 about = new AboutBox1();
about.Show();
}
private void button_Read_Click(object sender, EventArgs e)
{
Send_command(READSTATUS);
}
private void button_Cycle_Click(object sender, EventArgs e)
{
if (label_Status.Text == "IDLE")
{
if (V_Cycle < 8)
V_Cycle++;
else V_Cycle = 0;
switch (V_Cycle)
{
case 0:
Send_command(SETCYCLE0);
label_Cycle.Text = "Manual";
break;
case 1:
Send_command(SETCYCLE1);
label_Cycle.Text = "1 min";
break;
case 2:
Send_command(SETCYCLE2);
label_Cycle.Text = "2 min";
break;
case 3:
Send_command(SETCYCLE3);
label_Cycle.Text = "3 min";
break;
case 4:
Send_command(SETCYCLE4);
label_Cycle.Text = "4 min";
break;
case 5:
Send_command(SETCYCLE5);
label_Cycle.Text = "5 min";
break;
case 6:
Send_command(SETCYCLE10);
label_Cycle.Text = "10 min";
break;
case 7:
Send_command(SETCYCLE15);
label_Cycle.Text = "15 min";
break;
case 8:
Send_command(SETCYCLE30);
label_Cycle.Text = "30 min";
break;
default:
break;
}
}
}
private void Send_command(String command)
{
if (serialPort1.IsOpen)
{
serialPort1.Write(STX); // STX = 2
serialPort1.Write(command);
serialPort1.Write(ETX); // ETX = 3
}
}
private void button_Patient_Click(object sender, EventArgs e)
{
if (label_Status.Text == "IDLE")
{
if (label_Patient.Text == "Adult")
{
Send_command(SETNEO);
label_Patient.Text = "Neonate";
label_Pump.Text = "100 mmHg";
V_Pumpup = 0;
}
else if (label_Patient.Text == "Neonate")
{
Send_command(SETADULT);
label_Patient.Text = "Adult";
label_Pump.Text = "160 mmHg";
V_Pumpup = 3;
}
}
}
private void button_Pump_Click(object sender, EventArgs e)
{
if (label_Status.Text == "IDLE")
{
if (label_Patient.Text == "Neonate")
{
if (V_Pumpup < 2)
V_Pumpup++;
else
V_Pumpup = 0;
}
if (label_Patient.Text == "Adult")
{
if ((V_Pumpup < 4) && (V_Pumpup >= 2))
V_Pumpup++;
else
V_Pumpup = 2;
}
switch (V_Pumpup)
{
case 0:
Send_command(SETPMP100);
label_Pump.Text = "100 mmHg";
break;
case 1:
Send_command(SETPMP120);
label_Pump.Text = "120 mmHg";
break;
case 2:
Send_command(SETPMP140);
label_Pump.Text = "140 mmHg";
break;
case 3:
Send_command(SETPMP160);
label_Pump.Text = "160 mmHg";
break;
case 4:
Send_command(SETPMP180);
label_Pump.Text = "180 mmHg";
break;
}
}
}
private void button_Start_Click(object sender, EventArgs e)
{
if (label_Status.Text == "IDLE")
{
Send_command(STARTMEAS);
//Ser_Stat = RPRESS;
label_Status.Text = "MEASURE";
label_Statusstring.Text = "";
label_Sys.Text = "";
label_Dia.Text = "";
label_Pulse.Text = "";
}
}
private void button_Mano_Click(object sender, EventArgs e)
{
if (label_Status.Text == "IDLE")
{
Send_command(SETMANO);
label_Map.Text = "";
//Ser_Stat = RPRESS;
label_Status.Text = "Manometer";
}
}
private void button_Leak_Click(object sender, EventArgs e)
{
if (label_Status.Text == "IDLE")
{
if (label_Patient.Text == "Neonate")
{
button_Patient.PerformClick();
}
Send_command(SETLEAK);
//Ser_Stat = RPRESS;
label_Status.Text = "Leaktest";
label_Statusstring.Text = "";
label_Sys.Text = "";
label_Dia.Text = "";
label_Pulse.Text = "";
}
}
private void button_Break_Click(object sender, EventArgs e)
{
Send_command(STOPMEAS);
label_Status.Text = "IDLE";
V_Map = 0;
label_Sys.Text = "---";
label_Dia.Text = "---";
label_Pulse.Text = "---";
label_Map.Text = "---";
}
string buffer = "";
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
while (serialPort1.BytesToRead > 0)
{
buffer += serialPort1.ReadTo("\r");
int index1 = buffer.IndexOf('\u0002');
int index2 = buffer.IndexOf('\u0003', index1 + 1);
string buf = "";
if ((index1 >= 0) && (index2 > index1))
{
buf = buffer.Substring(index1 + 1, (index2 - 1 - index1));
buffer = buffer.Remove(index1, (index2 - index1));
this.BeginInvoke(new displayDeleg(display), new object[] { buf });
}
}
}
private void display(string message)
{
label_Statusstring.Text = message;
label_Statusstring.ForeColor = Color.Black;
if (message.Length > 3)
{
if ((message.Substring(5, 1)).Contains("S"))
{
string temp = message.Substring(6, 1);
switch (temp)
{
case "3":
label_Status.Text = "MEASURE";
break;
case "4":
label_Status.Text = "Manometer";
break;
case "7":
label_Status.Text = "Leaktest";
break;
default:
label_Status.Text = "IDLE";
break;
}
label_Map.Text = message.Substring(0, 3);
if (label_Map.Text != "")
V_Map = Convert.ToInt16(label_Map.Text);
if (V_Map < 300) { }
bar1.Value = V_Map;
}
else
{
if ((message.Substring(0, 1)).Contains("S"))
{
string temp = message.Substring(1, 1);
switch (temp)
{
case "2":
label_Statusstring.ForeColor = Color.Red;
break;
case "3":
label_Status.Text = "MEASURE";
break;
case "4":
label_Status.Text = "Manometer";
break;
case "7":
label_Status.Text = "Leaktest";
break;
default:
label_Status.Text = "IDLE";
break;
}
}
label_Map.Text = message.Substring(21, 3);
label_Sys.Text = message.Substring(15, 3);
label_Dia.Text = message.Substring(18, 3);
label_Pulse.Text = message.Substring(26, 3);
SaveData(
message.Substring(15, 3),
message.Substring(18, 3),
message.Substring(26, 3));
}
}
else if (message.Contains("999"))
{
Send_command(READSTATUS);
label_Status.Text = "IDLE";
bar1.Value = 0;
}
}
private void SaveData(string sys, string dia, string pulse)
{
try
{
string connectionString = @"Data Source=PLUTO-PC\;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\spiediena_merisana.mdf;Integrated Security=True;User Instance=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
string queryString = "INSERT INTO merisana1 (sys, dia, pulse) VALUES (@sys, @dia, @pulse)";
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@sys", sys);
command.Parameters.AddWithValue("@dia", dia);
command.Parameters.AddWithValue("@pulse", pulse);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel_time.Text = DateTime.Now.ToLongTimeString();
}
}
public class VerticalProgressBar : ProgressBar
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x04;
return cp;
}
}
}
}
最佳答案
你能试试这个吗:
private void SaveData(string sys, string dia, string pulse)
{
try
{
string connectionString = @"Data Source=(local);AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\spiediena_merisana.mdf;Integrated Security=True;User Instance=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
string queryString = "INSERT INTO merisana1 (sys, dia, pulse) VALUES (@sys, @dia, @pulse)";
SqlCommand command = new SqlCommand (queryString, connection);
command.Parameters.AddWithValue("@sys", sys);
command.Parameters.AddWithValue("@dia", dia);
command.Parameters.AddWithValue("@pulse", pulse);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
}
关于c# - 无法将子字符串发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16561504/
我的问题是如何在 python 中创建一个简单的数据库。我的例子是: User = { 'Name' : {'Firstname', 'Lastname'}, 'Address' : {'Street
我需要创建一个与远程数据库链接的应用程序! mysql 是最好的解决方案吗? Sqlite 是唯一的本地解决方案吗? 我使用下面的方法,我想知道它是否是最好的方法! NSString *evento
给定两台 MySQL 服务器,一台本地,一台远程。两者都有一个包含表 bohica 的数据库 foobar。本地服务器定义了用户 'myadmin'@'%' 和 'myadmin'@'localhos
我有以下灵活的搜索查询 Select {vt:code},{vt:productcode},{vw:code},{vw:productcode} from {abcd AS vt JOIN wxyz
好吧,我的电脑开始运行有点缓慢,所以我重置了 Windows,保留了我的文件。因为我的大脑还没有打开,所以我忘记事先备份我的 MySQL 数据库。我仍然拥有所有原始文件,因此我实际上仍然拥有数据库,但
如何将我的 Access 数据库 (.accdb) 转换为 SQLite 数据库 (.sqlite)? 请,任何帮助将不胜感激。 最佳答案 1)如果要转换 db 的结构,则应使用任何 DB 建模工具:
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
我想在相同的 phonegap 应用程序中使用 android 数据库。 更多说明: 我创建了 phonegap 应用程序,但 phonegap 应用程序不支持服务,所以我们已经在 java 中为 a
Time Tracker function clock() { var mytime = new Date(); var seconds
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
我有一个可以收集和显示各种测量值的产品(不会详细介绍)。正如人们所期望的那样,显示部分是一个数据库+建立在其之上的网站(使用 Symfony)。 但是,我们可能还会创建一个 API 来向第三方公开数据
我们将 SQL Server 从 Azure VM 迁移到 Azure SQL 数据库。 Azure VM 为 DS2_V2、2 核、7GB RAM、最大 6400 IOPS Azure SQL 数据
我正在开发一个使用 MongoDB 数据库的程序,但我想问在通过 Java 执行 SQL 时是否可以使用内部数据库进行测试,例如 H2? 最佳答案 你可以尝试使用Testcontainers Test
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 此问题似乎与 a specific programming problem, a sof
我正在尝试使用 MSI 身份验证(无需用户名和密码)从 Azure 机器学习服务连接 Azure SQL 数据库。 我正在尝试在 Azure 机器学习服务上建立机器学习模型,目的是我需要数据,这就是我
我在我的 MySQL 数据库中使用这个查询来查找 my_column 不为空的所有行: SELECT * FROM my_table WHERE my_column != ""; 不幸的是,许多行在
我有那个基地:http://sqlfiddle.com/#!2/e5a24/2这是 WordPress 默认模式的简写。我已经删除了该示例不需要的字段。 如您所见,我的结果是“类别 1”的两倍。我喜欢
我有一张这样的 table : mysql> select * from users; +--------+----------+------------+-----------+ | userid
我有表: CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL
我是一名优秀的程序员,十分优秀!