· 9 years ago · Oct 31, 2016, 09:44 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");
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 string strCreateTable = "CREATE TABLE myTable" +
45 "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +
46 "myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";
47 SqlConnection mConnection2 = new SqlConnection("Server=localhost;Initial Catalog=WorkplaceDatabase;Integrated security=SSPI;database=WorkplaceDatabase");
48 SqlCommand mc2 = new SqlCommand(strCreateTable, mConnection2);
49 try
50 {
51 mConnection.Open();
52 mCommand.ExecuteNonQuery();
53
54 mConnection2.Open();
55 mc2.ExecuteNonQuery();
56 /*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)";
57
58 using (SqlCommand command = new SqlCommand(commandStr, mConnection))
59 command.ExecuteNonQuery();
60
61 SqlCommand cmd = new SqlCommand("create table <Table Name>(empno int,empname varchar(50),salary money);", mConnection);
62 cmd.ExecuteNonQuery();*/
63
64 ///////3
65
66
67 sql1 = "INSERT INTO myTable(myId, myName, myAddress, myBalance) " +
68 "VALUES (1001, 'Puneet Nehra', 'A 449 Sect 19, DELHI', 23.98 ) ";
69 mc2 = new SqlCommand(sql1, mConnection2);
70 mc2.ExecuteNonQuery();
71
72 MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
73
74
75 }
76 catch (System.Exception ex)
77 {
78 MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
79 }
80 finally
81 {
82 if (mConnection.State == ConnectionState.Open)
83 {
84 mConnection.Close();
85 }
86 if (mConnection2.State == ConnectionState.Open)
87 {
88 mConnection2.Close();
89 }
90 }
91
92 }
93
94 private void button1_Click_1(object sender, EventArgs e)
95 {
96 string path = System.IO.Directory.GetCurrentDirectory();
97 path += "\\";
98 MessageBox.Show(path, "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
99
100 }
101 }
102}