GVKun编程网logo

在SQL Server中保存浮点值(sqlserver 浮点型)

26

此处将为大家介绍关于在SQLServer中保存浮点值的详细内容,并且为您解答有关sqlserver浮点型的相关问题,此外,我们还将为您介绍关于asp.net–在SQLServer中只获取浮点数的小数部

此处将为大家介绍关于在SQL Server中保存浮点值的详细内容,并且为您解答有关sqlserver 浮点型的相关问题,此外,我们还将为您介绍关于asp.net – 在SQL Server中只获取浮点数的小数部分、c# – Empty Textbox仍然在SQL Server中保存数据,即使表中的列也不允许为null、sql-server – OPENJSON在SQL Server中不起作用?、sql-server – 为什么我们需要在SQL Server中重建和重组索引的有用信息。

本文目录一览:

在SQL Server中保存浮点值(sqlserver 浮点型)

在SQL Server中保存浮点值(sqlserver 浮点型)

我有一个简单的Web应用程序,想要在SQL Server中保存一些Float或Double格式。

但是有一个问题,当我尝试保存123.66时,在表中我看到存储了123.6600003662109。

保存到数据库时,为什么我的浮点数发生了变化?我该如何解决该错误?

谢谢

答案1

小编典典

实际上 并没有尝试保存123.66,因为您不能完全以浮点数或双精度数表示123.66。仅此,数据库比以往更加准确地保存数据。

如果要准确保存 小数 ,请使用该decimal类型。

有关更多信息,请参见我有关浮动二进制点和浮动小数点类型的文章。

asp.net – 在SQL Server中只获取浮点数的小数部分

asp.net – 在SQL Server中只获取浮点数的小数部分

我在sql Server 2008中有以下编号:5.4564556
我只想从5.4564556获得4564556
如果我只能得到4564556中的4个是小数部分的第一个数字,那会更好.

我该怎么做?

谢谢

解决方法

你可以得到前四个小数部分:

select SUBSTRING (PARSENAME(5.4564556,1),1,4)

c# – Empty Textbox仍然在SQL Server中保存数据,即使表中的列也不允许为null

c# – Empty Textbox仍然在SQL Server中保存数据,即使表中的列也不允许为null

我正面临这个问题,当我在所有文本框都为空时单击“保存”按钮时,它会在所有文本框上显示星标.当我填写最后一个文本框而其他所有文本框都为空时,它会将数据保存到空字符串数据库中

我该如何处理这个问题?

if (tbIDCardNum.Text.Trim() == "")
{
    lblStarIDCardNum.Visibility = Visibility.Visible;
}

if (tbFirstName.Text.Trim() == "")
{
    lblStarFirstName.Visibility = Visibility.Visible;
}

if (rbMale.IsChecked == false && rbFemale.IsChecked == false)
{
    lblStarGender.Visibility = Visibility.Visible;
}

if (tbdob.Text == "")
{
    lblStardob.Visibility = Visibility.Visible;
}

if (tbDateOfJoining.Text == "")
{
    lblStarDOJ.Visibility = Visibility.Visible;
}

if (tbeducation.Text.Trim() == "")
{
    lblStarEducation.Visibility = Visibility.Visible;
}

if (tbCNIC.Text.Trim() == "")
{
    lblStarCNIC.Visibility = Visibility.Visible;
}

if (tbSalary.Text.Trim() == "")
{
    lblStarSalary.Visibility = Visibility.Visible;
}

if (tbAddress.Text.Trim() == "")
{
    lblStaraddress.Visibility = Visibility.Visible;
}

if (tbEmail.Text.Trim() == "")
{
    lblStarEmail.Visibility = Visibility.Visible;
}

if (tbContact1.Text.Trim() == "")
{
    lblStarContact.Visibility = Visibility.Visible;
}
else
{
    try
    {
        conn.open();

        cmd.CommandText = "insert into teacher (tIDCardNum,tFirstName,tLastName,tGender,tdob,tCNIC,tEducation,tSalary,tJoinedOn,tAddress,tEmail,tContact1,tContact2,tContact3,tStatus) values ('" + tbIDCardNum.Text.Trim() + "','" + tbFirstName.Text.Trim() + "','" + tbLastName.Text.Trim() + "','" + gender + "','" + tbdob.Text + "','" + tbCNIC.Text + "','" + tbeducation.Text + "','" + tbSalary.Text.Trim() + "','" + tbDateOfJoining.Text.Trim() + "','" + tbAddress.Text.Trim() + "','" + tbEmail.Text + "','" + tbContact1.Text + "','" + tbContact2.Text + "','" + tbContact3.Text + "',1)";

        cmd.Connection = conn;
        cmd.ExecuteNonQuery();

        cmd.Clone();
        conn.Close();

        HideStars();
        Refresh();

        MessageBox.Show("Saved");
   }
   catch (Exception ex)
   {
       if (ex.Message.Contains("Violation of PRIMARY KEY constraint "))
       {
           conn.Close();
           MessageBox.Show(ex.Message);
       }
       else
       {
           MessageBox.Show(ex.Message);
       }
   }
}

解决方法

正如我所理解的那样,似乎主要问题是你检查不同if语句中的所有字段,但只有最后一个有其他字段.正如我从你的帖子中假设的那样,这是你的问题;你想让每个文本框都有一个值,然后才开始在数据库中插入它,对吗?

如果将代码分解成更可重用的东西,偶然清理一些东西,可以更好地解释这一点.

首先,首先在类中引入一个变量,我们可以使用它来查看是否有空字段:

private bool HasEmptyFields = false;

接下来,让我们创建一个简单的帮助器,检查文​​本框是否为空/ null,更新相应标签的visiblity状态,如果确实为空则将’HasEmptyFields’设置为true:

private void ValidateField(TextBox textBox,Label label) {

    // check if the textBox actually is null - or empty (""),which is a difference
    // the nifty helper string.IsNullOrEmpty() will help with that
    var fieldisEmpty = string.IsNullOrEmpty(textBox.Text.Trim());

    // next,based on if the field is empty,set the visibility of the label
    // don't worry,this is fancy Syntax for a simple if...then...else
    label.Visibility = fieldisEmpty ? Visibility.Visible : Visibility.Hidden;

    if (fieldisEmpty) {
        // ONLY if this field is actually null,or empty,we make sure to 
        // inform the rest of the code this occ
        HasEmptyFields = true;
    }
}

有了这个,我们可以做一些事情:

ValidateField(tbIDCardNum,lblStarIDCardNum);
ValidateField(tbFirstName,lblStarFirstName);
// etc... continue doing this for all you fields

if (HasEmptyFields) {
    // ONLY if there is any field detected as being empty/null
    // we simply stop here (and skip the insert-into-db stuff)
    return;
} 

try 
{
    // if all fields indeed have a value,let's
    // continue with the insert-into-db stuff here

    conn.open();
    ...
}

现在有一些方法可以让它变得更漂亮.但这可能会帮助你朝着正确的方向前进.另外值得一提的是其他一些注释,比如阻止SQL injection(必然会发生),以及查看数据验证工具,这样您就不必自己编写所有这些验证代码.但显然,这不在这个答案的范围内.

sql-server – OPENJSON在SQL Server中不起作用?

sql-server – OPENJSON在SQL Server中不起作用?

我想在sql Server 2016中使用 JSON函数,但是当我尝试执行OPEnjsON函数时,会收到以下错误:

Msg 208,Level 16,State 1,Line 1
Invalid object name ‘openjson’.

为什么它不工作?我有sql Server 2016 RC版本.

解决方法

你可以检查数据库的兼容性级别吗? OPEnjsON在兼容级别130下可用.您可以尝试执行:
ALTER DATABASE database_name SET COMPATIBILITY_LEVEL = 130

另外,如果您在Azure sql数据库上使用JSON,请注意,即使新建数据库也是在120兼容级别下创建的,因此如果要使用OPEnjsON,则应更改该数据库.
此外,如果您在Azure sql数据库中使用它,请运行select @@ version查看是否为V12服务器.你应该看到像:

Microsoft sql Azure (RTM) – 12.0.2000.8
Mar 25 2016 15:11:30
copyright (c) Microsoft Corporation

如果您看到一些较低版本(例如11.xxx),则可能在不支持JSON的旧体系结构中可能有数据库.

问候,

约万

sql-server – 为什么我们需要在SQL Server中重建和重组索引

sql-server – 为什么我们需要在SQL Server中重建和重组索引

在搜索互联网后,我找不到原因

>为什么我们需要在sql Server中重建和重组索引?
>当我们重建和重组时,内部会发生什么?

关于site的文章说:

Index should be rebuild when index fragmentation is great than 40%. Index should be reorganized when index fragmentation is between 10% to 40%. Index rebuilding process uses more cpu and it locks the database resources. sql Server development version and Enterprise version has option ONLINE,which can be turned on when Index is rebuilt. ONLINE option will keep index available during the rebuilding.

我无法理解这一点,虽然它说什么时候这样做,但我想知道为什么我们需要重建和重组索引?

解决方法

在执行插入更新和删除操作时,索引将在内部和外部变为碎片.

内部碎片是指索引页面上的可用空间百分比很高,这意味着sql Server在扫描索引时需要读取更多页面.

外部碎片是当索引的页面不再有序时,因此sql Server必须做更多工作,特别是在IO术语中读取索引.

如果您的索引过于分散,最多,您的查询效率会降低,但最坏的情况是,sql Server将停止一起使用索引,这意味着几乎所有查询都必须执行表扫描或聚簇索引扫描.这会伤害你的表现很多!

重新组织索引时,sql Server使用现有的索引页面,只是在这些年龄段上随机播放数据.这将缓解内部碎片,还可以消除少量的外部碎片.它比重建更轻量级操作,并且始终在线.

重建索引时,sql Server实际上会对索引的数据进行重新分配并使用一组新的索引页.这显然会减轻内部碎片和外部碎片,但是这是一个更重的操作,默认情况下会使索引脱机,尽管它可以作为在线操作执行,具体取决于您的sql Server版本和设置.

但是,重建之后请不要期望有0碎片.除非您使用MAXDOP查询提示,否则sql Server将并行化重建操作并且涉及的处理器越多,可能存在的碎片就越多,因为每个处理器或核心将单独重建其索引的部分或片段,而不考虑彼此.这是在最佳碎片级别和重建索引所花费的时间之间的权衡.对于接近0的碎片,使用MAXDOP 1并在TempDB中对结果进行排序.

关于在SQL Server中保存浮点值sqlserver 浮点型的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于asp.net – 在SQL Server中只获取浮点数的小数部分、c# – Empty Textbox仍然在SQL Server中保存数据,即使表中的列也不允许为null、sql-server – OPENJSON在SQL Server中不起作用?、sql-server – 为什么我们需要在SQL Server中重建和重组索引等相关知识的信息别忘了在本站进行查找喔。

本文标签: