Database TABLE DataType

asked 2022-06-05T04:42:37.267+00:00
MiPakTeh 1,286 Reputation points

Hi All,
Can somebody show me how to change Data Type for Column ValueC to Double?
I use Convert.ToDouble it error.

Code;

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
using System.Data.SqlClient;  
  
namespace Store_4  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            SqlConnection conn = new SqlConnection(@"server=(localdb)\v11.0");  
            using (conn)  
            {  
                conn.Open();  
                string sql = string.Format(@"CREATE DATABASE [TRIAL] ON PRIMARY (NAME=TRIAL_a, FILENAME='{0}\TRIAL.mdf')  
                             LOG ON (NAME=TRIAL_log, FILENAME='{0}\TRIAL.ldf')", @"C:\Users\family\Documents\");  
                SqlCommand command = new SqlCommand(sql, conn);  
                command.ExecuteNonQuery();  
            }  
        }  
  
        private void button2_Click(object sender, EventArgs e)  
        {  
            SqlConnection conn = new SqlConnection(@"server=(localdb)\v11.0");  
            SqlCommand command = new SqlCommand("CREATE TABLE Items_( " + "PLACE varchar," + "ValueA decimal," + "ValueB decimal," + "ValueC Convert.ToDouble,);", conn);  
            conn.Open();  
            command.ExecuteNonQuery();  
            MessageBox.Show("Table has been created");  
        }  
  
        private void button3_Click(object sender, EventArgs e)  
        {  
  
        }  
  
        private void button4_Click(object sender, EventArgs e)  
        {  
  
  
        }  
    }  
}  


  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,923 questions
No comments
{count} votes

Accepted answer
  1. answered 2022-06-05T05:02:18.113+00:00
    sreejukg 9,166 Reputation points

    In SQL Server, to represent floating piont numbers, you can use float or real. Refer https://learn.microsoft.com/en-us/sql/t-sql/data-types/float-and-real-transact-sql?view=sql-server-ver16

    In your example, see the below query to create a table with real data type.

    CREATE TABLE Items (PLACE varchar ,ValueA decimal,ValueB decimal,ValueC real);   
    

    So your code can be

    SqlCommand command = new SqlCommand("CREATE TABLE Items (PLACE varchar ,ValueA decimal,ValueB decimal,ValueC real); ", conn);  
    

    since you are using varchar for place, it will create varchar(1), means your place can store only one char. If you are planning to store more characters in place, use your query as below.

    CREATE TABLE Items (PLACE varchar(200) ,ValueA decimal,ValueB decimal,ValueC real);  
    

    Hope this helps

    No comments

0 additional answers

Sort by: Most helpful