Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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# System.Data.SqlClient.SqlException(0x80131904):靠近'的语法不正确@med#U数据';_C#_Sql_Sql Server - Fatal编程技术网

C# System.Data.SqlClient.SqlException(0x80131904):靠近'的语法不正确@med#U数据';

C# System.Data.SqlClient.SqlException(0x80131904):靠近'的语法不正确@med#U数据';,c#,sql,sql-server,C#,Sql,Sql Server,我正在开发一个项目,您给了我一个错误: “System.Data.SqlClient.SqlException(0x80131904)“@med_Data”附近的语法不正确。” 但我不知道会出什么问题 string humidade = "", temperatura = "", heatIndex = "", lpgGas = "", monoCarbo = "", smoke = "

我正在开发一个项目,您给了我一个错误:

“System.Data.SqlClient.SqlException(0x80131904)“@med_Data”附近的语法不正确。”

但我不知道会出什么问题

string humidade = "", temperatura = "", heatIndex = "", lpgGas = "", monoCarbo = "", smoke = "";
            var macAddr =
            (
                from nic in NetworkInterface.GetAllNetworkInterfaces()
                where nic.OperationalStatus == OperationalStatus.Up
                select nic.GetPhysicalAddress().ToString()
            ).FirstOrDefault();
            Console.WriteLine("Mac adress do pc: {0}", macAddr);

                while (true)
                {
                    var dataRx = myPort.ReadLine();

                    var underscore = dataRx.Split('_');
                    humidade = underscore[0];
                    temperatura = underscore[1];
                    heatIndex = underscore[2];
                    lpgGas = underscore[3];
                    monoCarbo = underscore[4];
                    smoke = underscore[5];

                    Console.WriteLine(humidade);
                    Console.WriteLine(temperatura);
                    Console.WriteLine(heatIndex);
                    Console.WriteLine(lpgGas);
                    Console.WriteLine(monoCarbo);
                    Console.WriteLine(smoke);
                    Console.WriteLine("___________________");

                    string connString = "Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=tupaidb;Integrated Security=True";
                      SqlConnection conn = new SqlConnection(connString);
                    conn.Open();
                    if (conn.State==System.Data.ConnectionState.Open)
                    {
                        SqlDataAdapter cmd = new SqlDataAdapter();
                      cmd.InsertCommand = new SqlCommand ("INSERT INTO Bi_medicao VALUES (@med_hum, @med_temp, @med_lpg, @med_co, @med_fumo, @med_data");
                        cmd.InsertCommand.Connection = conn;
                        cmd.InsertCommand.Parameters.Add("@med_hum", humidade);
                        cmd.InsertCommand.Parameters.Add("@med_temp", temperatura);
                        cmd.InsertCommand.Parameters.Add("@med_lpg", lpgGas); 
                        cmd.InsertCommand.Parameters.Add("@med_co", monoCarbo);
                        cmd.InsertCommand.Parameters.Add("@med_fumo", smoke);
                        cmd.InsertCommand.Parameters.Add("@med_data", SqlDbType.Date);
                       
                        cmd.InsertCommand.ExecuteNonQuery();
                       conn.Close();

                    }

                
                
            }

a:您需要一个结束参数,b:当使用
插入
/
时,明确列顺序是一个非常好的主意:

//注意我在猜名字!
新建SqlCommand(@)
插入Bi_medicao(湿度、温度、液化石油气、一氧化碳、气体、数据)
数值(@med_-hum、@med_-temp、@med_-lpg、@med_-co、@med_-fumo、@med_-data)”;
还应该注意的是,这里使用时缺少了大量的
,并且不需要数据适配器。老实说,我会用简洁的方式:

使用简洁;//在文件的顶部,带有一个简洁的包ref
// ...
使用var conn=newsqlconnection(connString);
conn.Execute(@)
插入Bi_medicao(湿度、温度、液化石油气、一氧化碳、气体、数据)
数值(@humidade、@temperaturea、@lpgGas、@monoCarbo、@smoke、@data)”,
新{humidade,temperaturea,lpgGas,//参数
monoCarbo,烟雾,数据=DateTime.UtcNow});

还请注意,
SqlDbType.Date
的使用看起来非常错误-应该在某个地方有一个值。我已经猜到了
DateTime.UtcNow

你没有为你的
值关闭括号(
()
构造。我没有看到!顺便说一句,谢谢你,一旦你添加了右括号,我希望你会得到错误
参数化查询“…”需要参数“@med_data”,但没有提供该参数。
因为你从未给该参数赋值,所以你只需设置数据类型。如果要在单行中设置数据类型和值,可以使用:
cmd.InsertCommand.Parameters.Add(“@med_data”,SqlDbType.Date)。value=DateTime.UtcNow(将DateTime.Now替换为实际要插入的值)