Click here to Skip to main content
15,914,160 members
Home / Discussions / C#
   

C#

 
GeneralDataGridBoolColumn Pin
quocbao13-Jun-04 15:22
quocbao13-Jun-04 15:22 
GeneralRe: DataGridBoolColumn Pin
DougW4813-Jun-04 17:17
DougW4813-Jun-04 17:17 
GeneralRe: DataGridBoolColumn Pin
Heath Stewart13-Jun-04 18:47
protectorHeath Stewart13-Jun-04 18:47 
GeneralRe: DataGridBoolColumn Pin
DougW4814-Jun-04 9:22
DougW4814-Jun-04 9:22 
GeneralRe: DataGridBoolColumn Pin
Heath Stewart13-Jun-04 18:54
protectorHeath Stewart13-Jun-04 18:54 
Generalhelp about print Pin
ggl16513-Jun-04 15:01
ggl16513-Jun-04 15:01 
GeneralAttachment Database Pin
The_Soul_Of_Rock13-Jun-04 14:31
The_Soul_Of_Rock13-Jun-04 14:31 
GeneralRe: Attachment Database Pin
Heath Stewart13-Jun-04 18:45
protectorHeath Stewart13-Jun-04 18:45 
Use a SqlCommand to execute them, just like you'd type them in a command parser (like the Query Analyzer or osql.exe). Use a SqlConnection that specifies the 'master' database as the 'Initial Catalog', however. The 'master' database must always exist, otherwise you can't do anything, so it's a good database to attach to when running commands like that.

A simple example (without any error checking, like seeing if the files actually exist first):
public void AttachDatabase(string name, string mdfFile, string ldfFile)
{
  using (SqlConnection conn = new SqlConnection(
    "Data Source=.;Initial Catalog=master;Integrated Security=SSPI"))
  {
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "exec sp_attach_db @dbname, @filename1, @filename2";
    cmd.Parameters.Add("@dbname", SqlDbType.NVarChar, 128).Value = name;
    cmd.Parameters.Add("@filename1", SqlDbType.NVarChar, 260).Value = mdfFile;
    cmd.Parameters.Add("@filename2", SqlDbType.NVarChar, 260).Value = ldfFile;
 
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
  }
}
See the documentation for the sp_attach_db and sp_detach_db stored procs, as well as the SqlCommand.Parameters collection property for more information.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Attachment Database Pin
The_Soul_Of_Rock13-Jun-04 21:09
The_Soul_Of_Rock13-Jun-04 21:09 
GeneralDelete FileDialog Pin
anatsg13-Jun-04 7:49
anatsg13-Jun-04 7:49 
GeneralRe: Delete FileDialog Pin
Heath Stewart13-Jun-04 18:37
protectorHeath Stewart13-Jun-04 18:37 
GeneralRe: Delete FileDialog Pin
anatsg14-Jun-04 9:11
anatsg14-Jun-04 9:11 
Generalsome int16[ ] arrays: OutOfMemoryException Pin
noizy13-Jun-04 7:39
noizy13-Jun-04 7:39 
GeneralRe: some int16[ ] arrays: OutOfMemoryException Pin
Colin Angus Mackay13-Jun-04 12:52
Colin Angus Mackay13-Jun-04 12:52 
GeneralMenu Images in ContextMenu Pin
gek_at13-Jun-04 7:13
gek_at13-Jun-04 7:13 
GeneralRe: Menu Images in ContextMenu Pin
Mazdak13-Jun-04 7:27
Mazdak13-Jun-04 7:27 
GeneralRe: Menu Images in ContextMenu Pin
gek_at13-Jun-04 7:34
gek_at13-Jun-04 7:34 
GeneralRe: Menu Images in ContextMenu Pin
leppie13-Jun-04 10:20
leppie13-Jun-04 10:20 
GeneralRe: Menu Images in ContextMenu Pin
gek_at13-Jun-04 11:43
gek_at13-Jun-04 11:43 
GeneralRe: Menu Images in ContextMenu Pin
gek_at13-Jun-04 11:59
gek_at13-Jun-04 11:59 
GeneralRe: Menu Images in ContextMenu Pin
leppie13-Jun-04 12:19
leppie13-Jun-04 12:19 
GeneralRe: Menu Images in ContextMenu Pin
gek_at13-Jun-04 23:20
gek_at13-Jun-04 23:20 
GeneralRe: Menu Images in ContextMenu Pin
leppie13-Jun-04 12:02
leppie13-Jun-04 12:02 
General"an error occurred while loading the document" Pin
enduro1x13-Jun-04 6:35
enduro1x13-Jun-04 6:35 
GeneralRe: "an error occurred while loading the document" Pin
leppie13-Jun-04 6:59
leppie13-Jun-04 6:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.