The examples in this article describe how C # creates and appends databases. Share to everyone for your reference, specific as follows:
<summary>///Additional database methods///</summary>///<param name= "strSQL" > Connection database strings, connecting to the Master System database </param >///<param name= "dataname" > Database name </param>///<param name= "strmdf" > Database file MDF path </param>// /<param name= "strldf" > Database file ldf path </param>///<param name= "path" > installation directory </param> private void CreateDatabase (String strSQL, String dataname, String strmdf, String strldf, String path) {SqlConnection myconn = new
SqlConnection (strSQL);
String str = NULL;
try {str = "EXEC sp_attach_db @dbname = ' +dataname+" ', @filename1 = ' "+strmdf+" ', @filename2 = ' "+strldf+";
SqlCommand mycommand = new SqlCommand (str, myconn);
MyConn.Open ();
Mycommand.executenonquery (); MessageBox.Show ("Database installation successful!) Click OK to continue);//require using System.Windows.Forms} catch (Exception e) {MessageBox.Show ("Database installation failed!")
"+ e.message+" \ n "+" You can manually append data ");
System.Diagnostics.Process.Start (path);//Open installation directory} finally {myconn.close (); }} public OverrIDE void Install (System.Collections.IDictionary statesaver) {string Server = this. context.parameters["Server"];//server name string uid = this. context.parameters["User"];//sqlserver username string pwd = this. context.parameters["pwd"];//password string path = this. context.parameters["TARGETDIR"];//installation directory string strSQL = "server=" + server + "; uid=" + uid + ";p wd=" + pwd + ";d atabase=m
Aster ";//Connection database string string dataname =" JXC ";//Database name string strmdf = path + @" Jxc.mdf ";//mdf file path, you need to be aware that the filename is the same as the database file name you just added! String strldf = path + @ "Jxc_log.ldf";//ldf file path base.
Install (statesaver); This. CreateDatabase (strSQL, Dataname, Strmdf, strldf, path);/start creating database}///<summary>///test Connection///</summary>///& Lt;param name= "ServerName" ></param>///<param name= "dbname" ></param>///<param "Name=" UserName "></param>///<param name=" password "></param> private SqlConnection testconnection (
String ServerName, String dbname, String userName, string password) { String connectionString = GetConnectionString (ServerName, dbname, userName, password);
SqlConnection connection = new SqlConnection (connectionString); try {if (connection). State!= ConnectionState.Open) {connection.
Open ();
return connection;
catch {CloseConnection (connection); throw new Installexception ("Setup failed!\n database configuration is incorrect, please configure information correctly!")
"); }///<summary>///get connection string///</summary>///<param name= "ServerName" ></param>///<param N Ame= "dbname" ></param>///<param name= "UserName" ></param>///<param name= "password" >< /param>///<returns></returns> private String GetConnectionString (String serverName, String dbname, String userName, String password) {string connectionString = ' Data source={0};initial catalog={1}; User id={2};
Password={3} "; connectionString = string.
Format (connectionString, ServerName, dbname, userName, password); REturn connectionString; ///<summary>///CREATE database///</summary>///<param name= "ServerName" ></param>///<param name= "DBName" ></param>///<param name= "UserName" ></param>///<param name= "password" ></ param>///<param name= "Connection" ></param>///<param name= "statesaver" ></param> public
int CreateDatabase (SqlConnection connection) {int result =-1; Connection.
ChangeDatabase ("Master");
String createdbsql = @ ' If Exists (select 1 from sysdatabases where [name]=n ' {0} ') begin drop database {0}
End Go CREATE DATABASE {0} "; Createdbsql = string.
Format (Createdbsql, _dbname);
Because there is no go in the SqlCommand, so take the go for the separator SQL statement char[] split = new char[] {' G ', ' O '};
string[] sqllist = Createdbsql.split (Split);
SqlCommand command = NULL; try {command = connection.
CreateCommand (); Command.commandtype = System.Data.CommandType.TexT foreach (String Sqlitem in Sqllist) {if (Sqlitem.length > 2) {command.
CommandText = Sqlitem; result = command.
ExecuteNonQuery ();
} return result;
catch {CloseConnection (connection); Command.
Dispose ();
throw new Installexception ("Setup failed!\n database configuration is incorrect!"); }///<summary>///separate SQL statements///</summary>///<param name= "sql" ></param>///<returns>< ;/returns> Private string[] Splitsql (String sql) {regex regex = new Regex ("^go", Regexoptions.ignorecase |
Regexoptions.multiline); string[] Sqllist = regex. Split (SQL.
ToUpper ());
return sqllist;
}
Read more about C # Interested readers can view the site topics: "C # Programming Thread Usage Tips summary", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # tutorial on data structure and algorithms, summary of C # array manipulation techniques, and an introductory course on C # object-oriented programming
I hope this article will help you with C # programming.