ADO.NET: Создание БД и таблиц в ней
#37698365
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
Здравствуйте!
Стоит задача реализации Архива.
Для выполнения этой задачи, я программно, используя ADO.NET, пытаюсь создать БД и таблицы в ней.
Проблема: Таблицы в БД создаются через раз. База данных создается всегда, а вот структура БД остается пустой. При этом вылетает следующий Exception:
Ошибка на транспортном уровне при отправке запроса серверу. (provider: Shared Memory Provider, error: 0 - С обоих концов канала отсутствуют процессы.)
Метод для выполнения SQL-команд:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.
private void ExecuteDbCommand(string query, string connectionString) {
using (SqlConnection connection = new SqlConnection(connectionString)) {
SqlCommand myCommand = new SqlCommand(query, connection);
try {
connection.Open();
myCommand.ExecuteNonQuery();
} catch (Exception ex) {
string mes = ex.Message;
} finally {
if (connection.State == ConnectionState.Open)
connection.Close();
}
}
}
Запрос для создания БД:
1. 2. 3. 4. 5.
private void CreateDataBase(string name, string connectionString) {
string query = string.Format("CREATE DATABASE {0}", name);
ExecuteDbCommand(query, connectionString);
}
Запрос для создания таблиц в БД, которая создается методом выше:
1. 2. 3. 4. 5.
private void CreateTables(string connectionString) {
string query = "ТУТ SQL КОМАНДА ДЛЯ СОЗДАНИЯ ТАБЛИЦ (приведен ниже в спойлере)";
ExecuteDbCommand(query, connectionString);
}
Команда для создания таблиц в БД (приведен не полностью: в каждой таблице только по три столбца)
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. 292. 293. 294. 295. 296. 297. 298. 299. 300. 301. 302. 303. 304. 305. 306. 307. 308. 309. 310. 311. 312. 313. 314. 315. 316. 317. 318. 319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354. 355. 356. 357. 358. 359. 360. 361. 362. 363. 364. 365. 366. 367. 368. 369. 370. 371. 372. 373. 374. 375. 376. 377. 378. 379. 380. 381. 382. 383. 384. 385. 386. 387. 388. 389. 390. 391. 392. 393. 394. 395. 396. 397. 398. 399. 400. 401. 402. 403. 404. 405. 406. 407. 408. 409. 410. 411. 412. 413. 414. 415. 416. 417. 418. 419. 420. 421.
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
-- Creating table ''AccessDocuments''
CREATE TABLE [dbo].[AccessDocuments] (
[Id] uniqueidentifier NOT NULL,
[UserId] uniqueidentifier NOT NULL,
[ObjectId] uniqueidentifier NOT NULL
);
-- Creating table ''AccessTasks''
CREATE TABLE [dbo].[AccessTasks] (
[Id] uniqueidentifier NOT NULL,
[UserId] uniqueidentifier NOT NULL,
[ObjectId] uniqueidentifier NOT NULL
);
-- Creating table ''Activities''
CREATE TABLE [dbo].[Activities] (
[Id] uniqueidentifier NOT NULL,
[Text] nvarchar(max) NULL,
[Type] int NULL
);
-- Creating table ''Reports''
CREATE TABLE [dbo].[Reports] (
[Id] uniqueidentifier NOT NULL,
[Type] int NULL,
[Text] nvarchar(max) NULL
);
-- Creating table ''Settings''
CREATE TABLE [dbo].[Settings] (
[Id] uniqueidentifier NOT NULL,
[UniqueName] nvarchar(50) NULL,
[Type] nvarchar(50) NULL
);
-- Creating table ''Tasks''
CREATE TABLE [dbo].[Tasks] (
[Id] uniqueidentifier NOT NULL,
[Text] nvarchar(max) NULL,
[ExecutionDate] datetime NULL
);
-- Creating table ''PrismReports''
CREATE TABLE [dbo].[PrismReports] (
[Id] uniqueidentifier NOT NULL,
[Name] nvarchar(max) NULL,
[Category] nvarchar(max) NULL
);
-- Creating table ''Journals''
CREATE TABLE [dbo].[Journals] (
[Id] uniqueidentifier NOT NULL,
[DocumentId] uniqueidentifier NOT NULL,
[DocumentValue] varchar(max) NOT NULL
);
-- Creating table ''Templates''
CREATE TABLE [dbo].[Templates] (
[Id] uniqueidentifier NOT NULL,
[Name] nvarchar(max) NULL,
[Description] nvarchar(max) NULL
);
-- Creating table ''Histories''
CREATE TABLE [dbo].[Histories] (
[Id] int IDENTITY(1,1) NOT NULL,
[GroupId] uniqueidentifier NOT NULL,
[OperationId] nvarchar(max) NOT NULL
);
-- Creating table ''Employees''
CREATE TABLE [dbo].[Employees] (
[Id] uniqueidentifier NOT NULL,
[CreatedDate] datetime NOT NULL,
[ModifiedDate] datetime NOT NULL
);
-- Creating table ''Units''
CREATE TABLE [dbo].[Units] (
[Id] uniqueidentifier NOT NULL,
[CreatedDate] datetime NOT NULL,
[ModifiedDate] datetime NOT NULL
);
-- Creating table ''PermissionKeys''
CREATE TABLE [dbo].[PermissionKeys] (
[Id] int IDENTITY(1,1) NOT NULL,
[Key] nvarchar(max) NULL,
[Type] int NOT NULL
);
-- Creating table ''PermissionValues''
CREATE TABLE [dbo].[PermissionValues] (
[Id] int IDENTITY(1,1) NOT NULL,
[PermissionKey] nvarchar(max) NULL,
[Value] nvarchar(max) NULL
);
-- Creating table ''EmployeePermissions''
CREATE TABLE [dbo].[EmployeePermissions] (
[EmployeeId] uniqueidentifier NOT NULL,
[PermissionKey] nvarchar(max) NOT NULL,
[PermissionValue] nvarchar(max) NOT NULL
);
-- Creating table ''Dictionaries''
CREATE TABLE [dbo].[Dictionaries] (
[Id] uniqueidentifier NOT NULL,
[code] nvarchar(max) NULL,
[Name] nvarchar(max) NULL
);
-- Creating table ''Documents''
CREATE TABLE [dbo].[Documents] (
[Id] uniqueidentifier NOT NULL,
[IsDeleted] bit NOT NULL,
[IsAdministrativeUse] bit NOT NULL
);
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
-- Creating primary key on [Id] in table ''AccessDocuments''
ALTER TABLE [dbo].[AccessDocuments]
ADD CONSTRAINT [PK_AccessDocuments]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''AccessTasks''
ALTER TABLE [dbo].[AccessTasks]
ADD CONSTRAINT [PK_AccessTasks]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Activities''
ALTER TABLE [dbo].[Activities]
ADD CONSTRAINT [PK_Activities]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Reports''
ALTER TABLE [dbo].[Reports]
ADD CONSTRAINT [PK_Reports]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Settings''
ALTER TABLE [dbo].[Settings]
ADD CONSTRAINT [PK_Settings]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Tasks''
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [PK_Tasks]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''PrismReports''
ALTER TABLE [dbo].[PrismReports]
ADD CONSTRAINT [PK_PrismReports]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Journals''
ALTER TABLE [dbo].[Journals]
ADD CONSTRAINT [PK_Journals]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Templates''
ALTER TABLE [dbo].[Templates]
ADD CONSTRAINT [PK_Templates]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Histories''
ALTER TABLE [dbo].[Histories]
ADD CONSTRAINT [PK_Histories]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Employees''
ALTER TABLE [dbo].[Employees]
ADD CONSTRAINT [PK_Employees]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Units''
ALTER TABLE [dbo].[Units]
ADD CONSTRAINT [PK_Units]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''PermissionKeys''
ALTER TABLE [dbo].[PermissionKeys]
ADD CONSTRAINT [PK_PermissionKeys]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''PermissionValues''
ALTER TABLE [dbo].[PermissionValues]
ADD CONSTRAINT [PK_PermissionValues]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''EmployeePermissions''
ALTER TABLE [dbo].[EmployeePermissions]
ADD CONSTRAINT [PK_EmployeePermissions]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Dictionaries''
ALTER TABLE [dbo].[Dictionaries]
ADD CONSTRAINT [PK_Dictionaries]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- Creating primary key on [Id] in table ''Documents''
ALTER TABLE [dbo].[Documents]
ADD CONSTRAINT [PK_Documents]
PRIMARY KEY CLUSTERED ([Id] ASC);
-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------
-- Creating foreign key on [ObjectId] in table ''AccessTasks''
ALTER TABLE [dbo].[AccessTasks]
ADD CONSTRAINT [FK_AccessTasksObjectId_TasksId]
FOREIGN KEY ([ObjectId])
REFERENCES [dbo].[Tasks]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_AccessTasksObjectId_TasksId''
CREATE INDEX [IX_FK_AccessTasksObjectId_TasksId]
ON [dbo].[AccessTasks]
([ObjectId]);
-- Creating foreign key on [ActivityId] in table ''Tasks''
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Tasks_Activities]
FOREIGN KEY ([ActivityId])
REFERENCES [dbo].[Activities]
([Id])
ON DELETE CASCADE ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_Tasks_Activities''
CREATE INDEX [IX_FK_Tasks_Activities]
ON [dbo].[Tasks]
([ActivityId]);
-- Creating foreign key on [TaskId] in table ''Reports''
ALTER TABLE [dbo].[Reports]
ADD CONSTRAINT [FK_ReportsTaskId_TasksId]
FOREIGN KEY ([TaskId])
REFERENCES [dbo].[Tasks]
([Id])
ON DELETE CASCADE ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_ReportsTaskId_TasksId''
CREATE INDEX [IX_FK_ReportsTaskId_TasksId]
ON [dbo].[Reports]
([TaskId]);
-- Creating foreign key on [PositionId] in table ''Employees''
ALTER TABLE [dbo].[Employees]
ADD CONSTRAINT [FK_EmployeesPositionId_UnitsId]
FOREIGN KEY ([PositionId])
REFERENCES [dbo].[Units]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_EmployeesPositionId_UnitsId''
CREATE INDEX [IX_FK_EmployeesPositionId_UnitsId]
ON [dbo].[Employees]
([PositionId]);
-- Creating foreign key on [EmployeeId] in table ''Units''
ALTER TABLE [dbo].[Units]
ADD CONSTRAINT [FK_UnitsEmployeeId_EmployeesId]
FOREIGN KEY ([EmployeeId])
REFERENCES [dbo].[Employees]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_UnitsEmployeeId_EmployeesId''
CREATE INDEX [IX_FK_UnitsEmployeeId_EmployeesId]
ON [dbo].[Units]
([EmployeeId]);
-- Creating foreign key on [ParentId] in table ''Units''
ALTER TABLE [dbo].[Units]
ADD CONSTRAINT [FK_UnitsParentId_UnitsId]
FOREIGN KEY ([ParentId])
REFERENCES [dbo].[Units]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_UnitsParentId_UnitsId''
CREATE INDEX [IX_FK_UnitsParentId_UnitsId]
ON [dbo].[Units]
([ParentId]);
-- Creating foreign key on [ParentId] in table ''Dictionaries''
ALTER TABLE [dbo].[Dictionaries]
ADD CONSTRAINT [FK_Dictionaries_ParenId_Dictionaries]
FOREIGN KEY ([ParentId])
REFERENCES [dbo].[Dictionaries]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_Dictionaries_ParenId_Dictionaries''
CREATE INDEX [IX_FK_Dictionaries_ParenId_Dictionaries]
ON [dbo].[Dictionaries]
([ParentId]);
-- Creating foreign key on [ObjectId] in table ''AccessDocuments''
ALTER TABLE [dbo].[AccessDocuments]
ADD CONSTRAINT [FK_AccessDocumentsObjectId_DocumentsId]
FOREIGN KEY ([ObjectId])
REFERENCES [dbo].[Documents]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_AccessDocumentsObjectId_DocumentsId''
CREATE INDEX [IX_FK_AccessDocumentsObjectId_DocumentsId]
ON [dbo].[AccessDocuments]
([ObjectId]);
-- Creating foreign key on [DocumentId] in table ''Activities''
ALTER TABLE [dbo].[Activities]
ADD CONSTRAINT [FK_Activities_DocumentId_Documents]
FOREIGN KEY ([DocumentId])
REFERENCES [dbo].[Documents]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_Activities_DocumentId_Documents''
CREATE INDEX [IX_FK_Activities_DocumentId_Documents]
ON [dbo].[Activities]
([DocumentId]);
-- Creating foreign key on [TemplateId] in table ''Documents''
ALTER TABLE [dbo].[Documents]
ADD CONSTRAINT [FK_Documents_Template]
FOREIGN KEY ([TemplateId])
REFERENCES [dbo].[Templates]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_Documents_Template''
CREATE INDEX [IX_FK_Documents_Template]
ON [dbo].[Documents]
([TemplateId]);
-- Creating foreign key on [DocumentId] in table ''Reports''
ALTER TABLE [dbo].[Reports]
ADD CONSTRAINT [FK_Reports_DocumentId_Documents]
FOREIGN KEY ([DocumentId])
REFERENCES [dbo].[Documents]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_Reports_DocumentId_Documents''
CREATE INDEX [IX_FK_Reports_DocumentId_Documents]
ON [dbo].[Reports]
([DocumentId]);
-- Creating foreign key on [DocumentId] in table ''Tasks''
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Tasks_Documents]
FOREIGN KEY ([DocumentId])
REFERENCES [dbo].[Documents]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY ''FK_Tasks_Documents''
CREATE INDEX [IX_FK_Tasks_Documents]
ON [dbo].[Tasks]
([DocumentId]);
-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------
|
|