我可以知道热我可以在 Razor 中写这样的 c# Opendialog 吗?我正在尝试制作一个 openfiledialog,它可以让用户将照片上传到 SqlServerCe 数据库中:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
string path = openFileDialog1.FileName;
byte[] image = File.ReadAllBytes(path);
string query = "UPDATE firma SET logo=@Image WHERE id = 1";
SqlCommand sqlCommand = new SqlCommand(query, conn);
sqlCommand.Parameters.AddWithValue("@Image", image);
conn.Open();
sqlCommand.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}