Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在组合框中选择的本地计算机中下载文件?_C#_.net_Winforms - Fatal编程技术网

C# 如何在组合框中选择的本地计算机中下载文件?

C# 如何在组合框中选择的本地计算机中下载文件?,c#,.net,winforms,C#,.net,Winforms,我正在开发windows应用程序,其中有3个按钮,1个用于浏览,1个用于在sql server数据库中上载文件,1个用于从数据库中下载文件 有一个组合框,显示上传的数据库文件,用户可以选择该文件。我想下载选定的文件 可能吗? 如果是,那怎么办 您可以在数据库中使用FILESTREAM来存储文件或VARBINARY。 下面是一个使用VARBINARY的示例。 您可以通过许多方式来改进它,例如使用单个连接对象等 public Form1() { InitializeComponent();

我正在开发windows应用程序,其中有3个按钮,1个用于浏览,1个用于在sql server数据库中上载文件,1个用于从数据库中下载文件

有一个组合框,显示上传的数据库文件,用户可以选择该文件。我想下载选定的文件

可能吗?

如果是,那怎么办

您可以在数据库中使用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();
}
public Form1()
{
初始化组件();
}
私有无效浏览\单击(对象发送者,事件参数e)
{
openFileDialog1.Title=“添加文件”;
openFileDialog1.Filter=“所有文件(*.*)|*.*”;
openFileDialog1.FileName=“”;
openFileDialog1.Multiselect=true;
openFileDialog1.ShowDialog();
}
私有void openFileDialog1\u FileOk(对象发送方,CancelEventArgs e)
{
字符串[]sFilePath;
sFilePath=openFileDialog1.filename;
if(sFilePath.Length==0)
{
返回;
}
checkedListBox1.Items.AddRange(sFilePath);
}
私有无效上载\u单击(对象发送者,事件参数e)
{
SqlConnection thisConnection=new
SqlConnection(@“您的连接字符串”);
thisConnection.Open();
尝试
{
SqlCommand mySqlCommand=thisConnection.CreateCommand();
foreach(checkedListBox1.SelectedItems中的变量项)
{
字符串filepath=item.ToString();
如果(!File.Exists(filepath))
{
继续;
}
字符串filename=新文件信息(filepath).Name;
byte[]rawData=File.ReadAllBytes(文件路径);
mySqlCommand.CommandText=“插入文件([fname],[file])值(@fname,@file)”;
添加(“@fname”,SqlDbType.NVarChar);
mySqlCommand.Parameters.Add(“@file”,SqlDbType.VarBinary);
mySqlCommand.Parameters[“@fname”].Value=filename;
mySqlCommand.Parameters[“@file”].Value=rawData;
mySqlCommand.ExecuteNonQuery();
}
thisConnection.Close();
}
抓住
{
thisConnection.Close();
}
checkedListBox1.Items.Clear();
UpdateDropDown();
}
私有void UpdateDropDown()
{
comboBox1.Items.Clear();
SqlConnection thisConnection=newsqlconnection(@“您的连接字符串”);
thisConnection.Open();
string str=“从[文件]中选择id、fname”;
SqlCommand cmd=新的SqlCommand(str,thisConnection);
SqlDataAdapter da=新的SqlDataAdapter(cmd);
数据集ds=新数据集();
da.Fill(ds,“上传文件”);
foreach(ds.Tables[“uploadedfile”].Rows中的数据行)
{
comboBox1.Items.Add(新的{id=row[0],value=row[1]});
}
thisConnection.Close();
comboBox1.ValueMember=“id”;
comboBox1.DisplayMember=“值”;
comboBox1.Update();
}
私有无效按钮3\u单击(对象发送者,事件参数e)
{
int-selectedid=2;
如果(选择EDID<0)
{return;}
SaveFileDialog savefileDialog1=新建SaveFileDialog();
savefileDialog1.SupportMultiDottedExtensions=false;
savefileDialog1.Filter=“文本文件(*.txt)|*.txt |所有文件(*.*)|*.”;
savefileDialog1.Title=“将文件另存为…”;
DialogResult=savefileDialog1.ShowDialog();
if(result!=DialogResult.OK)
{
返回;
}
string filename=savefileDialog1.filename;
SqlConnection thisConnection=newsqlconnection(@“您的连接字符串”);
thisConnection.Open();
string str=“选择[file],fname FROM[Files],其中id=“+selectedid;
SqlCommand cmd=新的SqlCommand(str,thisConnection);
SqlDataAdapter da=新的SqlDataAdapter(cmd);
数据集ds=新数据集();
da.Fill(ds,“上传文件”);
if(ds.Tables[“uploadedfile”].Rows.Count>0)
{
字节[]bb=(字节[])ds.Tables[“uploadedfile”].行[0][“file”];
保存(文件名,bb);
}
thisConnection.Close();
}
私有void保存(字符串文件,字节[]数据)
{
//保存文件
}
私有void Form1\u加载(对象发送方、事件参数e)
{
UpdateDropDown();
}

“在sql server数据库中上载文件”?对不起,我不清楚你说的到底是什么意思。你的意思是“从备份文件恢复数据库”吗?或者…?您在数据库中使用FILESTREAM吗?