0

I have a windows form application created using C#. I have a few databases and tables in SQL server 2005 express edition. To create a setup of my project I wish to attach the databases and tables dynamically while my setup wizard runs on any remote machine.

I tried to write a code which will simply create database and also a try-catch statement which will check if the database exists but everytime execution of try catch statement whenever my application runs may be a problem.

Is this possible by simply attaching the .mdf files with my project as I create the setup wizard ?

Please help. Thanks in advance.

4

3 回答 3

2

创建表:

string createString = "CREATE TABLE myTable (column1 INT, column2 NVARCHAR(10))"; //YOUR SQL COMMAND TO CREATE A TABLE
SqlConnection connection = new SqlConnection(YOUR CONNECTION STRING);

SqlCommand create = new SqlCommand(createString , connection);

connection.Open();
create.ExecuteNonQuery();
connection.Close();
于 2013-03-29T10:49:19.863 回答
0

您可以将 .mdf 文件复制到根目录中,如果您进行设置,它将在您的设置文件中。我已经做到了。您的连接字符串将是: string.Format("{0}{1}{2}", @"Data Source=.\SQLEXPRESS;AttachDbFilename=", AppDomain.CurrentDomain.BaseDirectory, @"Database.mdf;Integrated Security=真;用户实例=假");

于 2013-06-11T11:03:46.973 回答
0
string myConnectionString= @"DataSource =...";

SqlConnection dbConnection = new SqlConnection(myConnectionString);

string myCommand = "CREATE TABLE myTable (column1 VARCHAR(10),colunm2 INT)";

SqlCommand dbConnection = new SqlCommand(myCommand,dbConnection);

try
{
dbConnection.Open();

dbCommand.ExecuteNonQuery();
}

catch{}

dbConnection.Close();
于 2013-09-27T20:10:12.370 回答