首页 新闻 会员 周边 捐助

如何用代码实现在应用程序当前目录新建sql server数据库?

0
悬赏园豆:50 [已解决问题] 解决于 2012-04-13 09:01

RT。我建了两次代码都不行,求大家指教,代码应该如何改
我建库的代码是这样子的:

string str = "CREATE DATABASE MyDatabase ON PRIMARY " + "(NAME = MyDatabase_Data, " + "FILENAME = '" + Application.ExecutablePath + "\\MyDatabaseData.mdf'," + "SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " + "LOG ON (NAME = MyDatabase_Log, " + "FILENAME = '" + Application.ExecutablePath+"\\MyDatabaseLog.ldf'," + "SIZE = 1MB, " + "MAXSIZE = 5MB, " + "FILEGROWTH = 10%)";

 

但出错了,在Application.ExecutablePath处显示:当前上下文不存在名称Application

改为:

string str = "CREATE DATABASE MyDatabase ON PRIMARY " + "(NAME = MyDatabase_Data, " + "FILENAME = Application.ExecutablePath+"+"\\MyDatabaseData.mdf,"+ "SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " + "LOG ON (NAME = MyDatabase_Log, " + "FILENAME = Application.ExecutablePath+" + "\\MyDatabaseLog.ldf," + "SIZE = 1MB, " + "MAXSIZE = 5MB, " + "FILEGROWTH = 10%)";

也不行。

jackyy的主页 jackyy | 初学一级 | 园豆:129
提问于:2012-04-12 21:48
< >
分享
最佳答案
0

是什么类型的程序(webform 或 winform)是否引用了“using System.Windows.Forms;”这个命名空间,写法应该是第一种,你断点调试一下

收获园豆:50
artwl | 专家六级 |园豆:16736 | 2012-04-12 21:59

控制台的。
确定就是在字符串里错了。

jackyy | 园豆:129 (初学一级) | 2012-04-12 22:35

解决了,原来要使用Application.ExecutablePath,必须要用Windows Form项目作开发才行。

另外建库的语句写成:

           string str = "CREATE DATABASE MyDatabase ON PRIMARY " +
           "(NAME = MyDatabase_Data, " +
           "FILENAME = '" + Application.ExecutablePath + "\\MyDatabaseData.mdf'," +
           "SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
           "LOG ON (NAME = MyDatabase_Log, " +
           "FILENAME = '" +          Application.ExecutablePath+"\\MyDatabaseLog.ldf'," +
           "SIZE = 1MB, " +
           "MAXSIZE = 5MB, " +
           "FILEGROWTH = 10%)";
jackyy | 园豆:129 (初学一级) | 2012-04-13 09:03

谢谢二楼的artwl:-)

jackyy | 园豆:129 (初学一级) | 2012-04-13 09:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册