我想檢查文件是否存在於數據庫或不使用asp.net。我搜索了一下,但我沒有發現與字節文件比較。如何使用查詢在數據庫中存在比較字節文件?
我使用了Visual Studio 2010,SQL Server 2008和C#語言。
於是,我試着寫這個代碼,但顯示錯誤:
Incorrect syntax near 'System.Byte[])'.
此外,有沒有關於這個問題的解決花葯?
代碼
if (ext == ".doc" || ext == ".docx" || ext == ".pdf" || ext == ".txt")
{
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//insert the file into database
strQuery = "insert into [Text File](User_id, T_Title, T_Extension, T_Data, Course_code, Course_num, T_Description, T_Keyword,Date)" +
" values (@User_id, @T_Title, @T_Extension, @T_Data, @Course_code, @Course_num, @T_Description, @T_Keyword, @Date)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@User_id", (string)Session["ID"]);
cmd.Parameters.Add("@T_Title", SqlDbType.VarChar).Value = filename;
cmd.Parameters.Add("@T_Extension", SqlDbType.VarChar).Value = ext;
cmd.Parameters.Add("@T_Data", SqlDbType.VarBinary).Value = bytes;
strQueryCount = "select count(*) from [Text File] where T_Data.SequenceEqual ('" + bytes + ")'";
cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@Course_code", Course_code.SelectedItem.Text);
cmd.Parameters.Add("@Course_num", Course_num.SelectedItem.Text);
cmd.Parameters.Add("@T_Description", Description.Text);
cmd.Parameters.Add("@T_Keyword", keywords.Text);
InsertUpdateData(cmd, bytes, strQueryCount);
}
private Boolean InsertUpdateData(SqlCommand cmd, Byte[] bytes, string strQueryCount)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
command = new SqlCommand(strQueryCount, con);
int num = Convert.ToInt16(command.ExecuteScalar());
Label2.Text = num.ToString();
if (num == 0)
{
cmd.ExecuteNonQuery();
return true;
}
else
{
Label2.ForeColor = System.Drawing.Color.Red;
Label2.Text = "error ";
Description.Text = " ";
keywords.Text = " ";
Course_code.SelectedItem.Text = " ";
Course_num.SelectedItem.Text = " ";
return false;
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
finally
{
con.Close();
con.Dispose();
}
}
謝謝..
文件通常不包含它們的文件名或甚至它們的擴展名。你究竟想要做什麼?如果您試圖通過其內容搜索二進制文件的類型,則必須非常熟悉文件格式。 –