C# 正在将注册表中的图像上载到本地数据库
我希望用户能够在注册时上传图像。我不太明白,希望你能帮忙。我正在使用asp.net c和Visual Studio中的本地数据库 这就是我目前所拥有的HTML:C# 正在将注册表中的图像上载到本地数据库,c#,asp.net,webforms,C#,Asp.net,Webforms,我希望用户能够在注册时上传图像。我不太明白,希望你能帮忙。我正在使用asp.net c和Visual Studio中的本地数据库 这就是我目前所拥有的HTML: <tr> <td class="auto-style3">Image </td> <td class="auto-style4"> <asp:FileUpload ID="FileUpload1" runat="server" /> 后端: protected void S
<tr>
<td class="auto-style3">Image </td>
<td class="auto-style4">
<asp:FileUpload ID="FileUpload1" runat="server" />
后端:
protected void Submit_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM Users WHERE ([Username] = @user)", conn);
check_User_Name.Parameters.AddWithValue("@user", TextBoxUN.Text);
int UserExist = (int)check_User_Name.ExecuteScalar();
if (UserExist > 0)
{
Response.Write("Username already exists");
}
else
{
string insertQuery = "insert into Users (FirstName,SecondName,UserName,Password,Email,ProductOwner,ScrumMaster,Developer,ConfirmPassword, Image) values(@FName,@SName,@UserName,@Password,@Email,@ProductOwner,@ScrumMaster,@Developer,@ConfirmPassword,@Image)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@FName", TextBoxFN.Text);
com.Parameters.AddWithValue("@SName", TextBoxSN.Text);
com.Parameters.AddWithValue("@UserName", TextBoxUN.Text);
com.Parameters.AddWithValue("@Password", TextBoxPass.Text);
com.Parameters.AddWithValue("@ConfirmPassword", TextBoxConPass.Text);
com.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@ProductOwner", CheckBoxProduct.Checked.ToString());
com.Parameters.AddWithValue("@ScrumMaster", CheckBoxScrum.Checked.ToString());
com.Parameters.AddWithValue("@Developer", CheckBoxDeveloper.Checked.ToString());
com.Parameters.Add("@Image");
</td>
</tr>
您需要将映像保存到磁盘上:
if (FileUpload1.HasFile)
{
// you have to create a folder in which to store the images
var savePath = Path.Combine(Server.MapPath("~/UserImages"), FileUpload1.FileName;
FileUpload1.SaveAs(savePath);
}
您需要在数据库中保存文件名:
com.Parameters.Add("@Image", FileUpload1.FileName);
您必须确保文件夹中不包含同名文件。您需要将映像保存到磁盘上:
if (FileUpload1.HasFile)
{
// you have to create a folder in which to store the images
var savePath = Path.Combine(Server.MapPath("~/UserImages"), FileUpload1.FileName;
FileUpload1.SaveAs(savePath);
}
您需要在数据库中保存文件名:
com.Parameters.Add("@Image", FileUpload1.FileName);
您必须确保文件夹中不包含同名文件。您遇到了什么问题\错误?您遇到了什么问题\错误?谢谢!虽然在单击submit:System.Data.SqlClient.SqlException时出现此错误:“操作数类型冲突:nvarchar与图像不兼容”您收到此错误是因为您在SQL中指定列为图像类型,这意味着您将实际图像存储在不推荐使用的数据库中。最佳做法是将图像文件存储在磁盘上,并在数据库中引用文件名之类的文件。为此,您需要更改列图像,并将类型从Image更改为nvarcharThank you!虽然在单击submit:System.Data.SqlClient.SqlException时出现此错误:“操作数类型冲突:nvarchar与图像不兼容”您收到此错误是因为您在SQL中指定列为图像类型,这意味着您将实际图像存储在不推荐使用的数据库中。最佳做法是将图像文件存储在磁盘上,并在数据库中引用文件名之类的文件。为此,您需要更改列映像,并将类型从Image更改为nvarchar