C# - Create SQL Server table programmatically

前端 未结 7 2348
温柔的废话
温柔的废话 2020-12-15 06:37

I am trying to create a SQL Server table programmatically. Here is the code.

using (SqlConnection con = new SqlConnection(conStr))
{

    try
    {
        /         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 07:03

    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace SqlCommend
    {
        class sqlcreateapp
        {
            static void Main(string[] args)
            {
                try
                {
                    SqlConnection conn = new SqlConnection("Data source=USER-PC; Database=Emp123;User Id=sa;Password=sa123");
                    SqlCommand cmd = new SqlCommand("create table (empno int,empname varchar(50),salary money);", conn);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    Console.WriteLine("Table Created Successfully...");
                    conn.Close();
                }
                catch(Exception e)
                {
                    Console.WriteLine("exception occured while creating table:" + e.Message + "\t" + e.GetType());
                }
                Console.ReadKey();
            }
        }
    }
    

    提交回复
    热议问题