即使表列设置为“allow null”,也无法在数据库中保存空值

时间:2014-02-09 13:24:56

标签: c# sql-server-2008-r2

我使用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;
}

我收到错误

  

“这条路不是合法的形式。”

在我的ReadFile()函数中,此FileInfo fInfo = new FileInfo(sPath);代码行发生错误,我知道我们不能为路径分配null,但这是否意味着我无法在数据库中保存null,如果它是路径?

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

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

3 个答案:

答案 0 :(得分:1)

您应该使用调试器并在行

上放置一个断点
FileInfo fInfo = new FileInfo(sPath);

您将看到sPath的值不是有效路径,并且在调试器调用窗口窗口中移动到调用ReadFile的方法,您将能够看到哪一行创建了问题(数据库中的一个路径是" ."或类似的路径不是有效路径。)

答案 1 :(得分:1)

是的,你可以在条件块中做很多事情, 您也可以使用if (txtpath.Text == "")

代替if (txtpath.Text == null)

答案 2 :(得分:0)

在传递插入查询之前,我检查了txtpath.Text是否为null,如果它为null,则表示我没有传递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();
    }
}