Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t向数据库发送子字符串_C#_Sql Server_Database - Fatal编程技术网

C# Can';t向数据库发送子字符串

C# Can';t向数据库发送子字符串,c#,sql-server,database,C#,Sql Server,Database,我的目标是将数据从C#发送到SQL server,但DB端不会发生任何事情。有什么问题吗?可能是数据库端的数据类型错误 获取字符串并发送它的代码 完整代码 namespace NIBP2PC { public partial class Form1 : Form { private delegate void displayDeleg(string message); const string STX = "\u0002"; //Start

我的目标是将数据从C#发送到SQL server,但DB端不会发生任何事情。有什么问题吗?可能是数据库端的数据类型错误

获取字符串并发送它的代码

完整代码

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;
            }
        }
    }
}
NIBP2PC
{
公共部分类Form1:Form
{
私有委托void displayDeleg(字符串消息);
常量字符串STX=“\u0002”//开始
常量字符串ETX=“\u0003”//结束
常量字符串STARTMEAS=“01;;D7”//命令值
常量字符串STOPMEAS=“X”;
常量字符串SETCYCLE0=“03;;D9”//手动模式
常量字符串SETCYCLE1=“04;;DA”//1分钟
常量字符串SETCYCLE2=“05;;DB”//2分钟
常量字符串SETCYCLE3=“06;;DC”//3分钟
常量字符串SETCYCLE4=“07;DD”//4分钟
常量字符串SETCYCLE5=“08;;DE”//5分钟
常量字符串SETCYCLE10=“09;;DF”//10分钟
常量字符串SETCYCLE15=“10;;D7”//15分钟
常量字符串SETCYCLE30=“11;;D8”//30分钟
常量字符串SETMANO=“14;;DB”//压力计模式
const string SETREBOOT=“15;;DC”//复位板
const string SETLEAK=“17;;DE”//泄漏测试
常量字符串READSTATUS=“18;;DF”;//读取结果
常量字符串SETPMP100=“19;;E0”;//设置启动压力100mmHg
常量字符串设置pmp120=“20;;D8”//设置启动压力120mmHg
常量字符串设置pmp140=“21;;D9”//设置启动压力140mmHg
常量字符串设置pmp160=“22;;DA”;//设置启动压力160mmHg
常量字符串SETPMP180=“23;;DB”;//设置启动压力180mmHg
const string Set成人=“24;”;DC;//设置成人模式
常量字符串SETNEO=“25;;DD”;//设置Neo模式
const byte INIT=0;//目前尚未测量
常量字节OK=1;//状态值
常量字节RSTAT=2;//读取状态
常量字节RPRESS=3;
字节V_循环;
字节V_Pumpup;
INTV_地图;
//私有VerticalProgressBar1=新的VerticalProgressBar();
公共表格1()
{
初始化组件();
list_comport();
}
私有无效列表_comport()
{
//获取串行端口名的列表。
string[]ports=SerialPort.GetPortNames();
//向控制台显示每个端口名称。
foreach(端口中的字符串端口)
{
portToolStripMenuItem.DropDownItems.Add(端口,null,新事件处理程序(端口点击));
}
}
私有无效端口\u单击(对象发送方,事件参数e)
{
if(serialPort1.IsOpen)
serialPort1.Close();
serialPort1.ReadBufferSize=64;
serialPort1.ReceivedBytestThreshold=2;
字符串端口=sender.ToString();
serialPort1.PortName=端口;
尝试
{
serialPort1.Open();
}
抓住
{
MessageBox.Show(“串行端口”+serialPort1.PortName+
“无法打开!”,“RS232测试仪”,
MessageBoxButtons.OK,MessageBoxIcon.Warning);
};
toolStripStatusLabel_Com.Text=端口;
发送_命令(SETCYCLE0);
发送_命令(set成人);
label_Status.Text=“空闲”;
label_Cycle.Text=“手动”;
label_Patient.Text=“成人”;
标签_Pump.Text=“160毫米汞柱”;
V_循环=0;
V_Pumpup=3;
发送_命令(SETPMP160);
Send_命令(READSTATUS);//读取状态值
}
private void exitToolStripMenuItem\u单击(对象发送者,事件参数e)
{
Application.Exit();
}
private void aboutToolStripMenuItem\u单击(对象发送者,事件参数e)
{
AboutBox1 about=新的AboutBox1();
about.Show();
}
私有无效按钮\u读取\u单击(对象发送者,事件参数e)
{
发送_命令(READSTATUS);
}
私有无效按钮\u循环\u单击(对象发送者,事件参数e)
{
如果(标签_Status.Text==“空闲”)
{
如果(V_循环<8)
V_循环++;
否则V_循环=0;
开关(V_循环)
{
案例0:
发送_命令(SETCYCLE0);
label_Cycle.Text=“手动”;
打破
案例1:
发送_命令(SETCYCLE1);
label_Cycle.Text=“1分钟”;
打破
案例2:
发送_命令(SETCYCLE2);
label_Cycle.Text=“2分钟”;
打破
案例3:
发送_命令(SETCYCLE3);
label_Cycle.Text=“3分钟”;
打破
案例4:
发送_命令(SETCYCLE4);
label_Cycle.Text=“4分钟”;
打破
案例5:
发送_命令(SETCYCLE5);
label_Cycle.Text=“5分钟”;
打破
案例6:
发送_命令(SETCYCLE10);
label_Cycle.Text=“10分钟”;
打破
案例7:
发送_命令(SETCYCLE15);
label_Cycle.Text=“15分钟”;
打破
案例8:
发送_命令(SETCYCLE30);
label_Cycle.Text=“30分钟”;
打破
违约:
打破
}
}
}
普里夫
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);
    }

}
SaveData(label_Sys.Text,
     label_Dia.Text,
     label_Pulse.Text);
using (SqlConnection connection = new SqlConnection(connectionString))
        {
           connection.Open();

         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.ExecuteNonQuery();
        }
 string connectionString = @"Data Source=PlUTO-PC\;Initial Catalog=merisana;Integrated Security=True";