Home>

c#create database:

///<summary>
///create database
///</summary>
///<param name="connstr">connection string</param>
///<param name="_ strdbname">Database name</param>
///<returns></returns>
private static bool createdatabase (string connstr, string _strdbname)
{
  bool bsuccess=false;
  try
  {
    using (sqlconnection conmaster=new sqlconnection (connstr))
    {
      conmaster.open ();
      //check if the database has existed first
      string strexist [email protected]"select * from dbo.sysdatabases where name =" "+ _strdbname + @" "";
      sqlcommand cmdexist=new sqlcommand (strexist, conmaster);
      sqldatareader readerexist=cmdexist.executereader ();
      bool bexist=readerexist.hasrows;
      readerexist.close ();
      if (bexist)
      {
        string strdel [email protected]"drop database" + _strdbname;
        sqlcommand cmddel=new sqlcommand (strdel, conmaster);
        cmddel.executenonquery ();
      }
      //create the database now;
      string strdatabase="create database [" + _strdbname + "]";
      sqlcommand cmdcreate=new sqlcommand (strdatabase, conmaster);
      cmdcreate.executenonquery ();
      conmaster.close ();
    }
    bsuccess=true;
  }
  catch (exception e)
  {
    throw e;
  }
  return bsuccess;
}

c#import sql script:

///<summary>
///import sql script
///</summary>
///<param name="sqlconnstring">connection database string</param>
///<param name="varfilename">script path</param>
///<returns></returns>
private static bool executesqlfile (string sqlconnstring, string varfilename)
{
  if (! file.exists (varfilename))
  {
    return false;
  }
  streamreader rs=new streamreader (varfilename, system.text.encoding.default);
  arraylist alsql=new arraylist ();
  string commandtext="";
  string varline="";
  while (rs.peek ()>-1)
  {
    varline=rs.readline ();
    if (varline == "")
    {
      continue;
    }
    if (varline!="go")
    {
      commandtext +=varline;
      commandtext +="\ r \ n";
    }
    else
    {
      commandtext +="";
    }
  }
  alsql.add (commandtext);
  rs.close ();
  try
  {
    executecommand (sqlconnstring, alsql);
    return true;
  }
  catch (exception ex)
  {
    throw ex;
  }
}
private static void executecommand (string sqlconnstring, arraylist varsqllist)
{
 using (sqlconnection conn=new sqlconnection (sqlconnstring))
 {
  conn.open ();
  //don "t use transaction, because some commands cannot execute in one transaction.
  //sqltransaction vartrans=conn.begintransaction ();
  sqlcommand command=new sqlcommand ();
  command.connection=conn;
  //command.transaction=vartrans;
  try
  {
   foreach (string varcommandtext in varsqllist)
   {
    command.commandtext=varcommandtext;
    command.executenonquery ();
   }
   //vartrans.commit ();
  }
  catch (exception ex)
  {
   //vartrans.rollback ();
   throw ex;
  }
  finally
  {
   conn.close ();
  }
 }
}
c
  • Previous Commonly used JavaScript functions and methods for daily collection
  • Next Share the three parsing modes of ajax