C#mysql参数值为空

本文关键字:参数 C#mysql | 更新日期: 2023-09-27 17:59:03

所以我试图传入点字符串的参数,我的所有列在我的DB中都被标记为不为空,但当我尝试运行下面的代码时,它告诉我列Lat不能为空。有什么东西我在俯瞰吗?

string latParameter = "'Point(" + latString + ")'";
            string lonParameter = "'Point(" + lonString + ")'";
            string mySQLfinishedProcessing = " insert into zipcodes " +
              "set zipcode = '" + zipcodeString + "'" +
              ",State = '" + StateString + "'" +
              ",City = '" + CityString + "'" +
              ",lat = GeomFromText(@latParam)"+
              ",lon = GeomFromText(@lonParam)"+
            ",StateCode = '" + StateCodeString2 + "'";
            //File.AppendAllText(@"C:'sqlupdater.txt", mySQLfinishedProcessing + ";'n");
            MySqlConnection configCON = new MySqlConnection(SQLStringClass.zipCONString);
            MySqlCommand CounterLogs = new MySqlCommand(mySQLfinishedProcessing, configCON);
            CounterLogs.Parameters.Add(new MySqlParameter("@latParam", latParameter));
            CounterLogs.Parameters.Add(new MySqlParameter("@lonParam", lonParameter));
            configCON.Open();
            CounterLogs.ExecuteNonQuery();
            configCON.Close();

是的,latString和lonString都有值。

string latParameter='点(40.92233)'string lonParameter='点(-72.63708)'

好吧,这似乎是因为只有一个对一个点的引用,我更改字符串latParameter='point(40.92233 12)'就可以了。但我真的想把我的纬度/经度都放在1分栏里吗?

尽管这在MySQl-Edit中运行良好,但在C#中仍然无法解决问题。问题是字符串lonParameter需要包含一个;

这是正确的代码。

 string latParameter = "Point(" + latString + " " +lonString+");";
            string mySQLfinishedProcessing = " insert into zipcodes " +
              "set zipcode = '" + zipcodeString + "'" +
              ",State = '" + StateString + "'" +
              ",City = '" + CityString + "'" +
              ",Location = GeomFromText(@latParam)"+
            ",StateCode = '" + StateCodeString2 + "'";
            //File.AppendAllText(@"C:'sqlupdater.txt", mySQLfinishedProcessing + ";'n");
            MySqlConnection configCON = new MySqlConnection(SQLStringClass.zipCONString);
            MySqlCommand CounterLogs = new MySqlCommand(mySQLfinishedProcessing, configCON);
            CounterLogs.Parameters.Add(new MySqlParameter("@latParam", latParameter));
            configCON.Open();
            CounterLogs.ExecuteNonQuery();
            configCON.Close();

C#mysql参数值为空

您正在使用错误的语法编写插入语句。它必须是

string mySQLfinishedProcessing = " insert into zipcodes(zipcode ,State ,City ,lat ,lon ,StateCode ) values ( " +
          "'" + zipcodeString + "'" +
          ",'" + StateString + "'" +
          ",'" + CityString + "'" +
          ",GeomFromText(@latParam)" +
          ",GeomFromText(@lonParam)" +
        ",'" + StateCodeString2 + "')";

如果GeomFromText是一个db函数,它可能会为您传递的任何内容返回null。