小弟要实现上传附件(主要是文本)并写入数据库,找了好多资料,学C#不久,没能完全消化,在写的过程中出了如下问题,望各位大哥不吝赐教,先谢了!“/DCIP”应用程序中的服务器错误。 
________________________________________
从 System.String 到 System.Byte[] 的转换无效。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 从 System.String 到 System.Byte[] 的转换无效。源错误: 行 86:  myExeSQL.Parameters["@Name"].Value= FName;
行 87:  connAttach.Open();
行 88:  myExeSQL.ExecuteNonQuery();
行 89:  connAttach.Close();
行 90:  }源文件: c:\inetpub\wwwroot\dcip\arc\arcrftodo.aspx.cs    行: 88

解决方案 »

  1.   

    主要代码如下!private void upfAttach()
    {
    string m_strCnn = "";
    string m_strServer = "";
    string m_strDatabase = "";
    string m_strUid = "";
    string m_strPwd = "";
    m_strServer = System.Configuration.ConfigurationSettings.AppSettings["servername"];
    m_strDatabase = System.Configuration.ConfigurationSettings.AppSettings["databasename"];
    m_strUid = System.Configuration.ConfigurationSettings.AppSettings["pwd"];
    m_strPwd = System.Configuration.ConfigurationSettings.AppSettings["uid"];
        

    //生成连接字符串
    m_strCnn = "server=" + m_strServer + ";uid=" + m_strUid + ";pwd=" + m_strPwd + ";database=" + m_strDatabase;
    SqlConnection connAttach = new SqlConnection(m_strCnn);
    // int temp=Int32.Parse(this.upAttach.PostedFile.InputStream.Length.ToString());
    int  temp;
    temp = Int32.Parse(upAttach.PostedFile.InputStream.Length.ToString());
    int  fsize = temp;
    byte[] Files;
    Files=new Byte[fsize];
    upAttach.PostedFile.InputStream.Read(Files, 0,fsize);

    string FName = upAttach.PostedFile.FileName;
    FName=FName.Substring(FName.LastIndexOf("\\")+1);
    string striSendNo = "10009";
    string sql = "insert into arcSendAttach(vcFileName,tContent,iSendNo) ";
    sql += "values(@Name,@content,'" + striSendNo +"')";
    SqlCommand myExeSQL = new SqlCommand(sql,connAttach);
    myExeSQL.Parameters.Add("@content",System.Data.SqlDbType.Image);
    myExeSQL.Parameters ["@content"].Value=Files;
    myExeSQL.Parameters.Add("@Name",System.Data.SqlDbType.Image);
    myExeSQL.Parameters["@Name"].Value= FName;
    connAttach.Open();
    myExeSQL.ExecuteNonQuery();
    connAttach.Close();
    }