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:
-
- Private VoidButton#click (ObjectSender, system. eventargs E)
-
-
-
- {
-
-
-
- // Open the database connection
-
-
- If(Conn. State! = Connectionstate. Open) Conn. open ();
-
-
-
- StringSQL ="Create database mydb on primary"+ "(Name = test_data,
-
-
-
- Filename ='C: \ mysql \ mydb_data.mdf', Size = 3,"+"Maxsize = 5,
-
-
-
- Filegrowth = 10%) log on"+"(Name = mydbb_log,
-
-
- Filename ='C: \ mysql \ mydb_log.ldf', Size = 3,"+"Maxsize = 20, filegrowth = 1 )";
-
-
-
- Cmd =NewSqlcommand (SQL, Conn );
-
-
-
- Try
-
-
-
- {
-
-
-
- Cmd. executenonquery ();
-
-
-
- }
-
-
- Catch(Sqlexception AE)
-
-
-
- {
-
-
-
- MessageBox. Show (AE. Message. tostring ());
-
-
-
- }
-
-
-
- }
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:
-
- Private VoidButton2_click (ObjectSender, system. eventargs E)
-
-
- {
-
-
-
- // Open the database connection
-
-
-
- If(Conn. State = connectionstate. Open) Conn. Close ();
-
-
-
- Connectionstring ="Integrated Security = sspi ;"+"Initial catalog = mydb ;"+"Data Source = localhost ;";
-
-
-
- Conn. connectionstring = connectionstring;
-
-
- Conn. open ();
-
-
-
- SQL ="Create table mytable"+"(Myid integer constraint pkeymyid primary key ,"+ "Myname char (50 ),
-
-
-
- Myaddress char (255), mybalance float )";
-
-
-
- Cmd =NewSqlcommand (SQL, Conn );
-
-
-
- Try
-
-
- {
-
-
-
- Cmd. executenonquery ();
-
-
-
- // Add records to the table
-
-
-
- SQL ="Insert into mytable (myid, myname, myaddress, mybalance )"+"Values (1001, 'puneet nehra ', 'a 449 sect 19, delhi', 23.98 )";
-
-
-
- Cmd =NewSqlcommand (SQL, Conn );
-
-
- Cmd. executenonquery ();
-
-
-
- SQL ="Insert into mytable (myid, myname, myaddress, mybalance )"+"Values (1002, 'anoop Singh ', 'lodi Road, delhi', 353.64 )";
-
-
-
- Cmd =NewSqlcommand (SQL, Conn );
-
-
-
- Cmd. executenonquery ();
-
-
- SQL ="Insert into mytable (myid, myname, myaddress, mybalance )"+"Values (1003, 'rakesh m', 'nag Chowk, jabalpur m. p. ', 43.43 )";
-
-
-
- Cmd =NewSqlcommand (SQL, Conn );
-
-
-
- Cmd. executenonquery ();
-
-
- SQL ="Insert into mytable (myid, myname, myaddress, mybalance )"+"Values (1004, 'madan Kesh ', '4th Street, Lane 3, delhi', 23.00 )";
-
-
-
- Cmd =NewSqlcommand (SQL, Conn );
-
-
-
- Cmd. executenonquery ();
-
-
-
- }
-
-
-
- Catch(Sqlexception AE)
-
-
- {
-
-
-
- MessageBox. Show (AE. Message. tostring ());
-
-
-
- }
-
-
-
- }