2010-06-23 40 views
0

我正在開發Windows應用程序,其中有3個按鈕,1個用於瀏覽,1個用於上載sql server數據庫中的文件,1個用於從數據庫下載文件。如何在組合框中選擇的本地機器下載文件?

有1個combobox顯示數據庫上傳的文件,用戶可以選擇該文件。我想下載選定的文件。

可能嗎?
如果是的話那麼怎麼樣?

+0

「上傳在SQL Server數據庫中的文件」?對不起,但我不清楚你的意思。你的意思是像'從備份文件恢復數據庫'?要麼...? – mickeyf 2010-06-23 14:15:32

+0

你在數據庫中使用FILESTREAM嗎? – Jay 2010-06-23 15:05:49

回答

2

您可以在數據庫中存儲FILESTREAM文件或VARBINARY。 以下是使用VARBINARY的示例。 可以提高它在很多方面像使用單個連接對象等

public Form1() 
{ 
    InitializeComponent(); 
} 

private void browse_Click(object sender, EventArgs e) 
{ 
    openFileDialog1.Title = "Add File"; 
    openFileDialog1.Filter = "All Files (*.*)|*.*"; 
    openFileDialog1.FileName = ""; 
    openFileDialog1.Multiselect = true; 
    openFileDialog1.ShowDialog(); 
} 

private void openFileDialog1_FileOk(object sender, CancelEventArgs e) 
{ 
    string[] sFilePath; 
    sFilePath = openFileDialog1.FileNames; 
    if (sFilePath.Length == 0) 
    { 
     return; 
    } 
    checkedListBox1.Items.AddRange(sFilePath); 

} 

private void upload_Click(object sender, EventArgs e) 
{ 
    SqlConnection thisConnection = new 
      SqlConnection(@"YOUR CONNECTION STRING"); 
    thisConnection.Open(); 
    try 
    { 

     SqlCommand mySqlCommand = thisConnection.CreateCommand(); 


     foreach (var item in checkedListBox1.SelectedItems) 
     { 

      string filepath = item.ToString(); 
      if (!File.Exists(filepath)) 
      { 
       continue; 
      } 
      string filename = new FileInfo(filepath).Name; 
      byte[] rawData = File.ReadAllBytes(filepath); 

      mySqlCommand.CommandText = "INSERT INTO Files ([fname],[file]) VALUES (@fname,@file)"; 
      mySqlCommand.Parameters.Add("@fname", SqlDbType.NVarChar); 
      mySqlCommand.Parameters.Add("@file", SqlDbType.VarBinary); 
      mySqlCommand.Parameters["@fname"].Value = filename; 
      mySqlCommand.Parameters["@file"].Value = rawData; 

      mySqlCommand.ExecuteNonQuery(); 
     } 
     thisConnection.Close(); 
    } 
    catch 
    { 
     thisConnection.Close(); 
    } 
    checkedListBox1.Items.Clear(); 

    UpdateDropDown(); 
} 

private void UpdateDropDown() 
{ 

    comboBox1.Items.Clear(); 
    SqlConnection thisConnection = new SqlConnection(@"YOUR CONNECTION STRING"); 
    thisConnection.Open(); 

    string str = "SELECT id, fname FROM [Files]"; 
    SqlCommand cmd = new SqlCommand(str, thisConnection); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds, "uploadedfile"); 

    foreach (DataRow row in ds.Tables["uploadedfile"].Rows) 
    { 
     comboBox1.Items.Add(new { id = row[0], value = row[1] }); 
    } 
    thisConnection.Close(); 
    comboBox1.ValueMember = "id"; 
    comboBox1.DisplayMember = "value"; 
    comboBox1.Update(); 
} 

private void button3_Click(object sender, EventArgs e) 
{ 
    int selectedid = 2; 
    if (selectedid < 0) 
    { return; } 


    SaveFileDialog savefileDialog1 = new SaveFileDialog(); 
    savefileDialog1.SupportMultiDottedExtensions = false; 
    savefileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; 
    savefileDialog1.Title = "Save file as..."; 

    DialogResult result = savefileDialog1.ShowDialog(); 

    if (result != DialogResult.OK) 
    { 
     return; 
    } 

    string filename = savefileDialog1.FileName; 

    SqlConnection thisConnection = new SqlConnection(@"YOUR CONNECTION STRING"); 
    thisConnection.Open(); 

    string str = "SELECT [file], fname FROM [Files] where id =" + selectedid; 
    SqlCommand cmd = new SqlCommand(str, thisConnection); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds, "uploadedfile"); 

    if (ds.Tables["uploadedfile"].Rows.Count > 0) 
    { 
     byte[] bb = (byte[])ds.Tables["uploadedfile"].Rows[0]["file"]; 
     Save(filename, bb); 
    } 

    thisConnection.Close(); 


} 
private void Save(string file, byte[] data) 
{ 
    // save file 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    UpdateDropDown(); 
} 
相關問題