カスタムアクションを使用して、Visual Studio2010Webセットアッププロジェクトから新しいデータベースを作成しようとしています。最初に、ユーザーがSQLサーバーの名前と認証モードを選択するFromを作成しました。次を使用してサーバー名を取得します。
public string ServerString { get; set; }
private void SetServerString()
{
string cs;
if (cbInstant.SelectedIndex == 0) // windows authentication
cs = cbServer.SelectedValue.ToString();
else
cs = string.Format(cbServer.SelectedValue + ";" + txtUser.Text + ";" + txtPassword.Text);
ServerString = cs;
}
private void btnNext_Click(object sender, EventArgs e)
{
SetServerString();
formContext.Parameters["ServerString"] = this.ServerString;
this.Close();
}
それはうまく機能し、たとえば「PCName\SQLEXPRESS」を取得します。
カスタムアクションの場合:
public override void Install(System.Collections.IDictionary stateSaver)
{
string serverString = "";
if (this.Context.Parameters["ServerString"] != null)
serverString = this.Context.Parameters["ServerString"];
base.Install(stateSaver);
MakeSQLDatabase(connectionString);
}
private void MakeSQLDatabase(string serverString)
{
FileInfo file = new FileInfo("M:\\script.sql");
string script = file.OpenText().ReadToEnd();
file.OpenText().Close();
SqlConnection sqlConnection = new SqlConnection();
SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
sqlConnection.ConnectionString = sqlConnectionStringBuilder.ToString();
Server server = new Server(sqlConnection);
server.ConnectionContext.ServerInstance = (serverString);
server.ConnectionContext.Connect();
if (server.Databases["databasename"] != null)
{
server.KillAllProcesses("databasename");
server.KillDatabase("databasename");
}
Database database = new Database(server, "databasename");
database.Create();
database.ExecuteNonQuery(script);
}
「サーバーが見つかりませんでしたか、アクセスできませんでした」というエラーが表示されますが、変数serverStringを次のように変更すると
@"PCName\SQLEXPRESS"
できます!!何が問題なのかわかりません。バージョン2とバージョン4の間に競合があるため、Microsoft.SqlServer.Management.Smoを使用できませんでした。app.confgを追加しても
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
私はあなたの援助に感謝します、またはあなたはこのタスクを達成するためのより良い解決策に私を導くことができます。前もって感謝します。