I'm unable to create a database when hosting a website on IIS Server. Locally it's working. I just want to execute this procedure via a URL.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.createdb
(
@cmd varchar(1000),
@dbname varchar(50)
)
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = @dbname)
BEGIN
SET @cmd=' CREATE database '+ @dbname;
EXEC (@cmd);
END
END
public void createDB(string _date)
{
myConnectionString = string.Empty;
string dbName = _date;
myConnectionString = "Data Source=localhost; initial catalog=xxxxxxxx;Persist Security Info=True; user id=xxxxxx; password=xxxxxxx; Connection Timeout=7000;";
using (SqlConnection conn = new SqlConnection(myConnectionString.ToString()))
{
DataSet ds = new DataSet();
conn.Open();
string sql = string.Empty;
try
{
SqlCommand Cmd = new SqlCommand("createdb", conn);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("@dbname", dbName);
Cmd.ExecuteNonQuery();
}
catch (Exception e)
{
string data = e.Message;
RecordErrorLog("CreateMyFile¶" + data + "¶" + myConnectionString);
}
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.