搜索墙首页  编程技术  java  asp  ajax  php  c/c#/c++  数据库  oracle  mysql  db2  操作系统  windows  linux  股吧
首页 > 编程 > C#/C/C++
 1         
1楼  yuanyuyuyu 2007-01-16 16:31:14

SqlConnection   conn   =   new   SqlConnection( "Data   Source=(local);initial   catalog=master;user   id=sa;pwd= ");
                        SqlCommand   command   =   new   SqlCommand( "create   database   ' "   +   DataBaseName   +   " ' ",   conn);
                        try  
                        {
                                conn.Open();
                                command.ExecuteNonQuery();
                       
                        }
                        catch   (Exception   ex)
                        {
                                throw   new   Exception(ex.Message);

                        }
                        finally
                        {
                                conn.Close();

                        }
  总是报错!可能command.ExecuteNonQuery();的原因
2楼  TheRule 2007-01-16 16:33:43

public   bool   CreatDatabase(string   DatabaseName,string   DataFile,string   LogFile)
{
string   str;
SqlConnection   myConn   =   new   SqlConnection   ( "Server=localhost;Integrated   security=SSPI;database=master ");

str   =   "CREATE   DATABASE   "+   DatabaseName   + "   ON   PRIMARY   "   +
"(NAME   =   MyDatabase_Data,   "   +
"FILENAME   =   ' "   +   DataFile   +   " ',   "   +
"SIZE   =   2MB,   MAXSIZE   =   10MB,   FILEGROWTH   =   10%)   "   +
"LOG   ON   (NAME   =   MyDatabase_Log,   "   +
"FILENAME   =   ' "   +   LogFile   +   " ',   "   +
"SIZE   =   1MB,   "   +
"MAXSIZE   =   5MB,   "   +
"FILEGROWTH   =   10%) ";

SqlCommand   myCommand   =   new   SqlCommand(str,   myConn);
try
{
myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show( "DataBase   is   Created   Successfully ",   "MyProgram ",   MessageBoxButtons.OK,   MessageBoxIcon.Information);
return   true;
}
catch   (System.Exception   ex)
{
MessageBox.Show(ex.ToString(),   "MyProgram ",   MessageBoxButtons.OK,   MessageBoxIcon.Information);
return   false;
}
finally
{
if   (myConn.State   ==   ConnectionState.Open)
{
myConn.Close();
}
}
}
3楼  glacier111 2007-01-16 16:33:46

USE   [master]   create   database   ' "   +   DataBaseName   +   " '
试试这个,不一定好使
4楼  lizhizhe2000 2007-01-16 16:35:06

不要加引号
5楼  yuanyuyuyu 2007-01-16 16:43:04

谢谢了!我是猪~~~55555555555   去掉破引号就好了
6楼  justrelax1 2008-12-20 20:39:02

该回复于2008-12-20 23:33:39被管理员删除
 1         
您的发言将按有关规定都会存档,您须为所发表后果负责,请您遵纪守法并注意语言文明。
标题:我想写一个创建数据库的语句在C#里怎么写?
热门关注
标题回复点击
c# 匿名方法 泛型委托 List<string>101615
我今年28了开始学C#,有前途吗?97831
c++ 输出图像到网页0519
OnServerclick 如何用ctrl+enter提交表单(分不够了)0489
C#技术资料 教程0488
c++ 中使用ChartDirector输出图表的网页0462
在C#中如何通过拼IP地址检测某台机器的网络连接是否畅通4457
各位哪里有下载 Visio 20051448
XPath Example<2>0378
做个调查,有多少人用C#做WINFORM?199349
搜索墙@2009 www.pkwall.com all rights reserved QQ:276471788 [京ICP备09111534号]
声明:本站部分数据来源于网络,仅供参考,如有版权问题,请联系我们,我们将及时删除!转载本站请注明来源