简体   繁体   English

在数据库C#中保存一个十进制值

[英]save a decimal value in database c#

I am trying to save a value from text box into sql database. 我正在尝试将文本框中的值保存到sql数据库中。 I am having the error as shown on the picture. 我遇到如图所示的错误。 my code below: 我的代码如下:

    public void datastore()
        {
            string Blerje, Shitje, Data;
            Blerje = usdollar_buy.Text;
            Shitje = usdollar_sell.Text;
            Data = dateTimePicker.Text;

            try
            {
                string constring = "Data Source=DELL;Initial Catalog=login_register;Integrated Security=True";

                /* Declaring Connection Variable */
                SqlConnection con = new SqlConnection(constring);
                String sql = "INSERT into [login_register].[dbo].[BlerjeShitje] values ('" + Blerje + "','" + Shitje + "','" + Data + "')";

                /* Checking Connection is Opend or not If its not open the Opens */
                if (con.State != ConnectionState.Open)
                    con.Open();

                SqlCommand cmd = new SqlCommand(sql, con);

                /* Executing Stored Procedure */
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Te dhenat u ruajten ne databaze");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

在此处输入图片说明

1. You might be having more columns in your table than mentioned values(3) in your query. 1.您的表中的列可能比查询中提到的values(3)多。 so it is always good to specify the column names in your query for which columns you are inserting the values. 因此,最好在查询中指定要插入值的列的列名。

Try This: 尝试这个:

INSERT INTO [TableName](COL1,COl2,COL3)
Values(Value1,Value2,Value3);

2. As you mentioned your columsn are decimals, you are inserting them as as strings by enclosing the values within single quotes. 2.正如您提到的列是小数,您通过将值括在单引号中将它们作为字符串插入。

You should not enclose the decima values within single quotes. 您不应将十进制值用单引号引起来。

Suggestion : Your query is open to SQL Injection Attacks. 建议:您的查询已对SQL注入攻击开放。 I Would suggest you to use the Parameterised queries to avoid them. 我建议您使用参数化查询来避免它们。

change the datatype to (18,6) or so, whichever is suitable for you, 将数据类型更改为(18,6)左右,以适合您的方式为准,

The second part of decimal data type shows how many digits do you require after the 'point'. 十进制数据类型的第二部分显示在“点”之后需要多少个数字。 In your case it's '0', so db is rounding it to nearest integer. 在您的情况下,它是“ 0”,因此db将其舍入到最接近的整数。

Source: http://msdn.microsoft.com/en-us/library/ms187746.aspx 来源: http//msdn.microsoft.com/en-us/library/ms187746.aspx

You are missing the fields in your insert statement. 您缺少insert语句中的字段。

The database will try to determine the right columns and their order, but if you don't deliver all fields in the appropriate order, your query will fail. 数据库将尝试确定正确的列及其顺序,但是,如果您未按适当的顺序交付所有字段,则查询将失败。

So in short: 简而言之:

  1. Deliver all fields in the correct order ; 以正确的顺序交付所有字段;
  2. Or: add the fields you want to fill in the insert . 或者:添加要填写的字段insert

Sample: 样品:

String sql = "INSERT into [login_register].[dbo].[BlerjeShitje] (Blerje, Shitje, Data) values ('" + Blerje + "','" + Shitje + "','" + Data + "')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM