温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:NetShopForge网上商店程序(VB)源码
当前文件路径:NetShopForge/Website/Install/RunSQL.aspx

1<% 2
// Sample code for executing a T-SQL file using an ASP.NET page 3
// Copyright (C) Microsoft Corporation, 2007. All rights reserved. 4
5
// Written as a sample with use in conjuction with the SQL Server Database Publishing Wizard 6
// For more information visit http://www.codeplex.com/sqlhost/ 7
8
// ************************************************************************** 9
// Note: Please ensure that you delete this page once your database has been published to the remote server 10
// ************************************************************************** 11
12
%> 13
14
<%@ Page Language="C#" AutoEventWireup="true" %> 15
<%@ Import Namespace="System.Data" %> 16
<%@ Import Namespace="System.Data.SqlClient" %> 17
<%@ Import Namespace="System.IO" %> 18
19
20
<% 21
// ************************************************************************** 22
// Update these variables here 23
// ************************************************************************** 24
25
// Filename of the T-SQL file you want to run 26
string fileName = @"<<YOUR_SCRIPTFILE>>.SQL"; 27
28
// Connection string to the server you want to execute against 29
string connectionString = @"Server=<<YOUR_SERVER>>;User ID=<<YOUR_USERNAME>>;Password=<<YOUR_PASSWORD>>;Initial Catalog=<<YOUR_DATABASE>>"; 30
31
// Timeout of batches (in seconds) 32
int timeout = 600; 33
34
35
%> 36
37
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 38
39
<html xmlns="http://www.w3.org/1999/xhtml" > 40
<head runat="server"> 41
<title>Executing T-SQL</title> 42
</head> 43
<body> 44
<form id="form1" runat="server"> 45
<div> 46
47
</div> 48
</form> 49
<% 50
SqlConnection conn = null; 51
try 52
{ 53
this.Response.Write(String.Format("Opening file {0}<BR>", fileName)); 54
55
// read file 56
using (StreamReader sr = new StreamReader(new FileStream(fileName, FileMode.Open))) 57
{ 58
this.Response.Write("Connecting to SQL Server database...<BR>"); 59
60
// Create new connection to database 61
conn = new SqlConnection(connectionString); 62
63
conn.Open(); 64
65
while (!sr.EndOfStream) 66
{ 67
StringBuilder sb = new StringBuilder(); 68
SqlCommand cmd = conn.CreateCommand(); 69
70
while (!sr.EndOfStream) 71
{ 72
string s = sr.ReadLine(); 73
if (s != null && s.ToUpper().Trim().Equals("GO")) 74
{ 75
break; 76
} 77
78
sb.AppendLine(s); 79
} 80
81
// Execute T-SQL against the target database 82
cmd.CommandText = sb.ToString(); 83
cmd.CommandTimeout = timeout; 84
85
cmd.ExecuteNonQuery(); 86
} 87
88
} 89
this.Response.Write("T-SQL file executed successfully"); 90
} 91
catch (Exception ex) 92
{ 93
this.Response.Write(String.Format("An error occured: {0}", ex.ToString())); 94
} 95
finally 96
{ 97
// Close out the connection 98
// 99
if (conn != null) 100
{ 101
try 102
{ 103
conn.Close(); 104
conn.Dispose(); 105
} 106
catch (Exception e) 107
{ 108
this.Response.Write(String.Format(@"Could not close the connection. Error was {0}", e.ToString())); 109
} 110
} 111
} 112
113
114
%> 115
</body> 116
</html> 117



