# SQL Server database creation analysis

Source: Internet
Author: User

C # create an SQL Server database

After creating a database connection object, we canProgramUse it. First, we dynamically create an SQL Server database in C # in the program. We create the database in the C: \ mysql directory. to practice this instance, you must create a folder named MySQL under C:; otherwise, an error will occur! The key to creating a database is the SQL object in the function. Through this object, we specify some basic attributes of the database file. Then, we create a new sqlcommand object, through which we actually complete the operations on the database. The function is implemented as follows:

 

 
 
  1. Private VoidButton#click (ObjectSender, system. eventargs E)
  2.  
  3. {
  4.  
  5. // Open the database connection 
  6.  
  7. If(Conn. State! = Connectionstate. Open) Conn. open ();
  8.  
  9. StringSQL ="Create database mydb on primary"+ "(Name = test_data,
  10.  
  11. Filename ='C: \ mysql \ mydb_data.mdf', Size = 3,"+"Maxsize = 5,
  12.  
  13. Filegrowth = 10%) log on"+"(Name = mydbb_log,
  14.  
  15. Filename ='C: \ mysql \ mydb_log.ldf', Size = 3,"+"Maxsize = 20, filegrowth = 1 )";
  16.  
  17. Cmd =NewSqlcommand (SQL, Conn );
  18.  
  19. Try 
  20.  
  21. {
  22.  
  23. Cmd. executenonquery ();
  24.  
  25. }
  26.  
  27. Catch(Sqlexception AE)
  28.  
  29. {
  30.  
  31. MessageBox. Show (AE. Message. tostring ());
  32.  
  33. }
  34.  
  35. }

After creating a database, we need to create a table for it, which is the basic object in the database. The SQL statement CREATE TABLE is used to create a table. After the table is created, the schema is determined ). Then, four records are added to the table using the insert statement for future use. The function is implemented as follows:

 
 
  1. Private VoidButton2_click (ObjectSender, system. eventargs E)
  2.  
  3. {
  4.  
  5. // Open the database connection 
  6.  
  7. If(Conn. State = connectionstate. Open) Conn. Close ();
  8.  
  9. Connectionstring ="Integrated Security = sspi ;"+"Initial catalog = mydb ;"+"Data Source = localhost ;";
  10.  
  11. Conn. connectionstring = connectionstring;
  12.  
  13. Conn. open ();
  14.  
  15. SQL ="Create table mytable"+"(Myid integer constraint pkeymyid primary key ,"+ "Myname char (50 ),
  16.  
  17. Myaddress char (255), mybalance float )";
  18.  
  19. Cmd =NewSqlcommand (SQL, Conn );
  20.  
  21. Try 
  22.  
  23. {
  24.  
  25. Cmd. executenonquery ();
  26.  
  27. // Add records to the table 
  28.  
  29. SQL ="Insert into mytable (myid, myname, myaddress, mybalance )"+"Values (1001, 'puneet nehra ', 'a 449 sect 19, delhi', 23.98 )";
  30.  
  31. Cmd =NewSqlcommand (SQL, Conn );
  32.  
  33. Cmd. executenonquery ();
  34.  
  35. SQL ="Insert into mytable (myid, myname, myaddress, mybalance )"+"Values (1002, 'anoop Singh ', 'lodi Road, delhi', 353.64 )";
  36.  
  37. Cmd =NewSqlcommand (SQL, Conn );
  38.  
  39. Cmd. executenonquery ();
  40.  
  41. SQL ="Insert into mytable (myid, myname, myaddress, mybalance )"+"Values (1003, 'rakesh m', 'nag Chowk, jabalpur m. p. ', 43.43 )";
  42.  
  43. Cmd =NewSqlcommand (SQL, Conn );
  44.  
  45. Cmd. executenonquery ();
  46.  
  47. SQL ="Insert into mytable (myid, myname, myaddress, mybalance )"+"Values (1004, 'madan Kesh ', '4th Street, Lane 3, delhi', 23.00 )";
  48.  
  49. Cmd =NewSqlcommand (SQL, Conn );
  50.  
  51. Cmd. executenonquery ();
  52.  
  53. }
  54.  
  55. Catch(Sqlexception AE)
  56.  
  57. {
  58.  
  59. MessageBox. Show (AE. Message. tostring ());
  60.  
  61. }
  62.  
  63. }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.