2014-02-09 33 views
-1

我正在使用C#windows窗體應用程序將文件上載到SQL Server,但用戶不會總是上傳文件,如果用戶直接按下保存而不使用OpenFileDialog應該保存在空數據庫即使表列設置爲「允許null」,也無法保存數據庫中的空值

我保存按鈕的代碼是

private void btnSave_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     //Read File Bytes into a byte array for attaching file 
     byte[] FileData = ReadFile(txtpath.Text); 
     con.Open(); 
     cmd = new SqlCommand("insert into CourierMaster(srno,OriginalPath,FileData)values(" + txtsrNo.Text + ",@OriginalPath, @FileData)", con); 
     cmd.Parameters.Add(new SqlParameter("@OriginalPath", (object)txtpath.Text)); 
     cmd.Parameters.Add(new SqlParameter("@FileData", (object)FileData)); 
     int n = cmd.ExecuteNonQuery(); 
     if (n > 0) 
     { 
      MessageBox.Show("Record for "+txtsrNo.Text+" Inserted Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
     else 
      MessageBox.Show("Insertion failed"); 
    } 
    catch (SqlException ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
    finally 
    { 
     con.Close(); 
    } 
} 

我READFILE()函數是

byte[] ReadFile(string sPath) 
{ 
    //Initialize byte array with a null value initially. 
    byte[] data = null; 

    //Use FileInfo object to get file size. 
    FileInfo fInfo = new FileInfo(sPath); 
    long numBytes = fInfo.Length; 

    //Open FileStream to read file 
    FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read); 

    //Use BinaryReader to read file stream into byte array. 
    BinaryReader br = new BinaryReader(fStream); 

    //When you use BinaryReader, you need to supply number of bytes to read from file. 
    //In this case we want to read entire file. So supplying total number of bytes. 
    data = br.ReadBytes((int)numBytes); 

    //Close BinaryReader 
    br.Close(); 

    //Close FileStream 
    fStream.Close(); 
    return data; 
} 

我得到è RROR

「的路徑是不是合法的形式。」

在我的ReadFile()函數錯誤發生在這個FileInfo fInfo = new FileInfo(sPath);行代碼,我知道我們不能空分配給路徑,但這是否意味着如果它是一個路徑,我不能在我的數據庫中保存空值?

以下是我的應用程序的快照:

Application which accepts serial number through textbox and file through openfiledialogue

以下是我的表扣:

My table where srno is of numeric(18, 0), OriginalPath IS of varchar(MAX), FileData is of image datatype

+0

當您從ReadFile中得到錯誤時,爲什麼您會懷疑數據庫是根本原因?我希望'sPath'無效。檢查它包含的內容。 – usr

+0

調試時sPath包含一個空值,即「」 –

回答

0

通過插入查詢之前我查txtpath.Text是否爲空或不是,如果是空比我不及格OriginalPath和的FileData,這裏是代碼..

private void btnSave_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     if (txtpath.Text == "") 
     { 
      con.Open(); 
      cmd = new SqlCommand("insert into CourierMaster(srno)values(" + txtsrNo.Text +")", con); 
     } 
     else 
     { 
      byte[] FileData = ReadFile(txtpath.Text);       
      con.Open(); 
      cmd = new SqlCommand("insert into CourierMaster(srno, OriginalPath,FileData)values(" + txtsrNo.Text + ",@OriginalPath, @FileData)", con); 
      cmd.Parameters.Add(new SqlParameter("@OriginalPath", (object)txtpath.Text)); 
      cmd.Parameters.Add(new SqlParameter("@FileData", (object)FileData)); 
     } 
     int n = cmd.ExecuteNonQuery(); 
     if (n > 0) 
     { 
      MessageBox.Show("Record for "+txtsrNo.Text+" Inserted Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
     else 
      MessageBox.Show("Insertion failed"); 
    } 
    catch (SqlException ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
    finally 
    { 
     con.Close(); 
    } 
} 
1

您應該使用調試器,並放置一個斷點上線

FileInfo fInfo = new FileInfo(sPath); 

您會看到sPath的值不是有效路徑,並且在調試器調用堆棧窗口中移動到調用ReadFile的方法,您將能夠看到哪條線路創建了問題(您的路徑之一db是" ."或類似那不是有效路徑的東西)。

+0

而路徑的調試值是「」 –

+2

而且它是一個無效值的路徑,所以無論你的db值是空字符串還是空行值。你不應該在這種情況下調用你的''ReadFile''方法('string.IsNullOrEmpty'可以告訴你是否是這種情況) –

+0

那麼我需要檢查sPath是否爲null或不是即時消息ReadFile()函數? –

1

是的,你可以做很多條件塊中的東西, 而不是if (txtpath.Text == "")也可以使用if (txtpath.Text == null)

相關問題