本文介绍了插入文件时出错“列名称或提供的值的数量与表定义不匹配”请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void  Button1_Click(对象发​​件人,EventArgs e)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.InitialDirectory = Directory.GetCurrentDirectory();
if (openFileDlg.ShowDialog()== DialogResult.OK)
{
FileInfo fi = new FileInfo(openFileDlg.FileName);
FileStream fs = new FileStream(fi.FullName,FileMode.Open,FileAccess.Read);
BinaryReader rdr = new BinaryReader(fs);
byte [] fileData = rdr.ReadBytes(( int )fs.Length);
rdr.Close();
fs.Close();

string cs = @ Data Source = .\SQLEXPRESS; AttachDbFilename = | DataDirectory | \ SATDB.mdf; Integrated Security = True; User Instance = True;
使用(SqlConnection con = new SqlConnection(cs))
{
con.Open();
string sql = 插入SAT VALUES (@Trial_Addmission,默认);
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.Add( @ Trial_Addmission,SqlDbType.VarBinary,fileData.Length ).Value = fileData;
cmd.ExecuteNonQuery();
con.Close();
}
}

}
解决方案



private void Button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.InitialDirectory = Directory.GetCurrentDirectory();
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                FileInfo fi = new FileInfo(openFileDlg.FileName);
                FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
                BinaryReader rdr = new BinaryReader(fs);
                byte[] fileData = rdr.ReadBytes((int)fs.Length);
                rdr.Close();
                fs.Close();

                string cs = @"Data            Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SATDB.mdf;Integrated Security=True;User Instance=True";
                using (SqlConnection con = new SqlConnection(cs))
                {
                    con.Open();
                    string sql = "INSERT INTO SAT VALUES (@Trial_Addmission, default)";
                    SqlCommand cmd = new SqlCommand(sql, con);
                    cmd.Parameters.Add("@Trial_Addmission", SqlDbType.VarBinary, fileData.Length).Value = fileData;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }

        }
解决方案



这篇关于插入文件时出错“列名称或提供的值的数量与表定义不匹配”请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:25