· 9 years ago · Oct 31, 2016, 09:08 AM
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10using System.Data.SqlClient;
11
12namespace SqlScript_task3
13{
14 public partial class Form1 : Form
15 {
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21 private void button1_Click(object sender, EventArgs e)
22 {
23 string pathProject = System.IO.Directory.GetCurrentDirectory();
24 string DataPathName = pathProject + "\\WorkplaceDatabaseData.mdf";
25
26 string LogPathName = pathProject + "\\WorkplaceDatabaseLog.ldf";
27 // C://WorkplaceDatabaseData.mdf
28
29 String str;
30 SqlConnection mConnection = new SqlConnection("Server=localhost;Integrated security=SSPI;database=master");
31
32 str = "CREATE DATABASE WorkplaceDatabase ON PRIMARY " +
33 "(NAME = WorkPlaceDatabase_Data, " +
34 "FILENAME = '" + DataPathName + "', " +
35 "SIZE = 6MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
36 "LOG ON (NAME = WorkPlaceDatabase_Log, " +
37 "FILENAME = '" + LogPathName + "', " +
38 "SIZE = 1MB, " +
39 "MAXSIZE = 5MB, " +
40 "FILEGROWTH = 10%)";
41 string sql1;
42 SqlCommand mCommand = new SqlCommand(str, mConnection);
43
44
45 try
46 {
47 mConnection.Open();
48 mCommand.ExecuteNonQuery();
49
50 sql1 = "CREATE TABLE myTable" +
51 "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +
52 "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";
53 mCommand = new SqlCommand(sql1, mConnection);
54 /*var commandStr = "If not exists (select name from sysobjects where name = 'Customer') CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime)";
55
56 using (SqlCommand command = new SqlCommand(commandStr, mConnection))
57 command.ExecuteNonQuery();
58
59 SqlCommand cmd = new SqlCommand("create table <Table Name>(empno int,empname varchar(50),salary money);", mConnection);
60 cmd.ExecuteNonQuery();*/
61
62 ///////3
63
64 mCommand.ExecuteNonQuery();
65
66 sql1 = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " +
67 "VALUES (1001, 'Puneet Nehra', 'A 449 Sect 19, DELHI', 23.98 ) ";
68 mCommand = new SqlCommand(sql1, mConnection);
69 mCommand.ExecuteNonQuery();
70 MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
71
72
73 }
74 catch (System.Exception ex)
75 {
76 MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
77 }
78 finally
79 {
80 if (mConnection.State == ConnectionState.Open)
81 {
82 mConnection.Close();
83 }
84 }
85
86 }
87
88 private void button1_Click_1(object sender, EventArgs e)
89 {
90 string path = System.IO.Directory.GetCurrentDirectory();
91 path += "\\";
92 MessageBox.Show(path, "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
93
94 }
95 }
96}