如何在c#中将字符串值转换为float?

时间:2016-09-18 10:00:50

标签: c# c#-4.0

我在数据库中有一个float类型的列。我需要将从文本框中取出的浮点值传递给sql采石场,以将该值插入数据库中的表。 然而转换,

Convert.ToSingle(textbox1.Text)

无效。

我也试过了float num = float.Parse(textBox1.Text);。在两种方式中都会显示类型转换错误。

在microsoft sql server management 2014中,数据类型double不可用。

任何帮助都将受到高度赞赏。

代码: private void button5_Click(object sender,EventArgs e)         {

        if (conn.State.ToString() == "Closed")
        {
            conn.Open();
        }


        SqlCommand newCmd = conn.CreateCommand();
        newCmd.Connection = conn;
        newCmd.CommandType = CommandType.Text;
        newCmd.CommandText = "insert into salesTable(cusName,cusId,drugName,drugId,strength,quantity,unitPrice,receiptNo) values('" + Convert.ToString(metroTextBox1.Text) + "','" + Convert.ToInt32(metroTextBox2.Text) + "','" + Convert.ToString(metroComboBox1.SelectedItem) + "','" + Convert.ToInt32(metroTextBox4.Text) + "','" + Convert.ToString(metroTextBox5.Text) + "','" + Convert.ToInt32(metroTextBox6.Text) + "','" + Convert.ToSingle(metroTextBox7.Text) + "','" + Convert.ToInt32(metroTextBox8.Text) + "')";
        newCmd.ExecuteNonQuery();


        conn.Close();
        ClearAllText(this);
        comboFill();
        MessageBox.Show("sale has been recorded.");
    }

0 个答案:

没有答案