我用savefiledialog控件制作另存为,当保存时如果已经存在相同的文件时点覆盖程序就崩溃了?
这是怎么回事啊?

解决方案 »

  1.   

     private void button6_Click(object sender, EventArgs e)
            {
                if (pathtb.Text.Trim() == "")
                {
                    MessageBox.Show("请选择要上传的文件");
                }
                else
                {
                    saveFileDialog1.InitialDirectory = "f:\\";
                    saveFileDialog1.Filter = "图片(*.jpg)|*.jpg|pdf文件(*.pdf)|*.pdf";
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        
                        File.Copy(pathtb.Text, saveFileDialog1.FileName);
                        
                        string sql = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
                        string str = @"Data Source=zhang-pc\sql2005;Initial Catalog=档案管理;uid=sa;password=sa1234";
                        SqlConnection strconn = new SqlConnection(str);
                        strconn.Open();
                        SqlCommand com = new SqlCommand("update tb_roll set 档案文件='" + saveFileDialog1.FileName + "'where 案卷编号='" + sql + "'", strconn);                    com.ExecuteReader();
                        strconn.Close();
                        MessageBox.Show("上传成功");                }
                }
            }
    如果保存时要保存的文件没有相同的名字就能不会出现问题。