【发布时间】:2021-08-08 19:40:21
【问题描述】:
我的代码工作正常,但当我尝试从数据库中检索浮点数/卷轴号时,它只返回整数部分,如屏幕截图所示。 数据库中的值为 145.55,但文本框仅显示 145。
*我尝试使用 MessageBox 以确保问题不在文本框中,而是在 sql 查询返回的值中。
表格说明
Sql查询结果
表单结果
iteminformation.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Posfaction
{
public partial class frmItemInformation : Form
{
string fileExtension = ".jpg";
string ITEM_ID, WarehouseID;
public frmItemInformation(String VAR_ITEM_ID, String VAR_WarehouseID)
{
InitializeComponent();
ITEM_ID = VAR_ITEM_ID.ToString();
WarehouseID = VAR_WarehouseID.ToString();
txtItemID.Text = VAR_ITEM_ID.ToString();
}
private void ItemDescription() {
try
{
clsUtility.ExecuteSQLQuery("SELECT * FROM ItemInformation WHERE ITEM_ID ='" + ITEM_ID + "' ");
if (clsUtility.sqlDT.Rows.Count > 0)
{
txtItemName.Text = clsUtility.sqlDT.Rows[0]["ItemName"].ToString();
txtUnit.Text = clsUtility.sqlDT.Rows[0]["UnitOfMeasure"].ToString();
txtBatch.Text = clsUtility.sqlDT.Rows[0]["Batch"].ToString();
cmbGroup.SelectedValue = clsUtility.sqlDT.Rows[0]["GROUP_ID"].ToString();
cmbDefaultWarehouse.SelectedValue = clsUtility.sqlDT.Rows[0]["WarehouseID"].ToString();
txtBarcode.Text = clsUtility.sqlDT.Rows[0]["Barcode"].ToString();
//txtPurchaseCost.Text = clsUtility.sqlDT.Rows[0]["Cost"].ToString();
txtPurchaseCost.Text = Convert.ToString(clsUtility.sqlDT.Rows[0]["Cost"]);
txtSalesPrice.Text = clsUtility.sqlDT.Rows[0]["Price"].ToString();
txtReorderPoint.Text = clsUtility.sqlDT.Rows[0]["ReorderPoint"].ToString();
if (clsUtility.sqlDT.Rows[0]["VAT_Applicable"].ToString() == "Y") { cbVATapplicable.Checked = true; }
else { cbVATapplicable.Checked = false; }
if (clsUtility.sqlDT.Rows[0]["Barcode_Applicable"].ToString() == "Y") { chkApplicableBarcode.Checked = true; }
else { chkApplicableBarcode.Checked = false; }
try
{
pictureBox1.ImageLocation = Application.StartupPath + @"\Upload\ItemImage\" + clsUtility.sqlDT.Rows[0]["PhotoFileName"].ToString();
pictureBox1.InitialImage.Dispose();
fileExtension = Path.GetExtension(clsUtility.sqlDT.Rows[0]["PhotoFileName"].ToString());
}
catch (Exception) { pictureBox1.Image = Posfaction.Properties.Resources.No_image_found; }
}
clsUtility.ExecuteSQLQuery("SELECT * FROM Stock WHERE ITEM_ID ='" + ITEM_ID + "' AND WarehouseID ='" + WarehouseID + "' ");
if (clsUtility.sqlDT.Rows.Count > 0)
{
cmbWarehouse.SelectedValue = clsUtility.sqlDT.Rows[0]["WarehouseID"].ToString();
cmbShelf.SelectedValue = clsUtility.sqlDT.Rows[0]["SHELF_ID"].ToString();
txtOpeningStock.Text = clsUtility.sqlDT.Rows[0]["Quantity"].ToString();
try { dtpExpDate.Text = clsUtility.sqlDT.Rows[0]["ExpiryDate"].ToString(); }
catch (Exception) { }
if (clsUtility.sqlDT.Rows[0]["Expiry"].ToString() == "Y") { chkExp.Checked = true; }
else { chkExp.Checked = false; }
}
btnSubmit.Enabled = false;
btnDelete.Enabled = true;
btnAlter.Enabled = true;
}
catch (Exception) { }
}
private void frmItemInformation_Load(object sender, EventArgs e)
{
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
btnReset.PerformClick();
LoadLanguegePack();
if (ITEM_ID == "0") { }
else { ItemDescription(); }
}
}
clsUtility.cs
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Xml;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Windows.Forms;
using System.Data.SQLite;
//using MySql.Data.MySqlClient;
//using System.Data.SqlClient;
namespace Posfaction
{
class clsUtility
{
public static string CnString = Properties.Settings.Default.App_Conn_string;
public static DataTable sqlDT = new DataTable();
public static DataTable sqlDT2 = new DataTable();
public static string UserID;
public static string UserName;
public static string UsersPrivilege;
// Initializing Database Connection
public static bool DBConnectionInitializing()
{
bool functionReturnValue = false;
try
{
SQLiteConnection sqlCon = new SQLiteConnection();
sqlCon.ConnectionString = CnString;
sqlCon.Open();
functionReturnValue = true;
sqlCon.Close();
}
catch (Exception ex)
{
functionReturnValue = false;
Properties.Settings.Default.App_Default_Conn = false;
Properties.Settings.Default.Save();
MessageBox.Show("Error : " + ex.Message, "Error establishing the database connection..", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(0);
}
return functionReturnValue;
}
public static DataTable ExecuteSQLQuery(string SQLQuery)
{
try
{
SQLiteConnection sqlCon = new SQLiteConnection(CnString);
SQLiteDataAdapter sqlDA = new SQLiteDataAdapter(SQLQuery, sqlCon);
SQLiteCommandBuilder sqlCB = new SQLiteCommandBuilder(sqlDA);
sqlDT.Reset();
sqlDA.Fill(sqlDT);
}
catch (Exception ex)
{
Properties.Settings.Default.App_Default_Conn = false;
Properties.Settings.Default.Save();
MessageBox.Show("Error : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return sqlDT;
}
}
}
【问题讨论】:
-
你试过
((double)clsUtility.sqlDT.Rows[0]["Cost"]).ToString()吗? -
请使用参数化查询 - 通过连接等方式构建 SQL 查询是灾难的根源。它不仅是许多难以调试的语法错误的来源,而且还是 SQL Injection attacks 的敞开大门。
-
((double)clsUtility.sqlDT.Rows[0]["Cost"]).ToString() ;我只是按照你的要求试了一下,没有任何变化。同样的问题。
-
值如何存储在数据库中(字符串或数字)?在 GUI 中看到逗号并不意味着该值作为字符串存储在数据库中。您没有在查询中使用参数,因此数据类型可能无法在数据库和 c# 应用程序之间正确传输。
标签: c# visual-studio sqlite