C# sql数据库中的映像路径
我正在做一个用户配置文件,首先用户选择图片并用此代码上传到文件夹中,上传后显示图像:C# sql数据库中的映像路径,c#,asp.net,sql,C#,Asp.net,Sql,我正在做一个用户配置文件,首先用户选择图片并用此代码上传到文件夹中,上传后显示图像: protected void btnUpload_Click(object sender, EventArgs e) { // Initialize variables string sSavePath; string sThumbExtension; int intThumbWidth; int intThumbHeight; // Set constant
protected void btnUpload_Click(object sender, EventArgs e)
{
// Initialize variables
string sSavePath;
string sThumbExtension;
int intThumbWidth;
int intThumbHeight;
// Set constant values
sSavePath = "images/";
sThumbExtension = "_thumb";
intThumbWidth = 160;
intThumbHeight = 120;
// If file field isn’t empty
if (filUpload.PostedFile != null)
{
// Check file size (mustn’t be 0)
HttpPostedFile myFile = filUpload.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
{
lblOutput.Text = "El archivo no fue cargado.";
return;
}
// Check file extension (must be JPG)
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
{
lblOutput.Text = "El archivo debe tener una extensión JPG";
return;
}
// Read file into a data stream
byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
// Make sure a duplicate file doesn’t exist. If it does, keep on appending an
// incremental numeric until it is unique
string sFilename = System.IO.Path.GetFileName(myFile.FileName);
int file_append = 0;
while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
{
file_append++;
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ file_append.ToString() + ".jpg";
}
// Save the stream to disk
System.IO.FileStream newFile
= new System.IO.FileStream(Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
// Check whether the file is really a JPEG by opening it
System.Drawing.Image.GetThumbnailImageAbort myCallBack =
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap;
try
{
myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename));
// If jpg file is a jpeg, create a thumbnail filename that is unique.
file_append = 0;
string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ sThumbExtension + ".jpg";
while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
{
file_append++;
sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
file_append.ToString() + sThumbExtension + ".jpg";
}
// Save thumbnail and output it onto the webpage
System.Drawing.Image myThumbnail
= myBitmap.GetThumbnailImage(intThumbWidth,
intThumbHeight, myCallBack, IntPtr.Zero);
myThumbnail.Save(Server.MapPath(sSavePath + sThumbFile));
imgPicture.ImageUrl = sSavePath + sThumbFile;
// Displaying success information
lblOutput.Text = "El archivo fue cargado con exito!";
// Destroy objects
myThumbnail.Dispose();
myBitmap.Dispose();
}
catch (ArgumentException errArgument)
{
// The file wasn't a valid jpg file
lblOutput.Text = "No es un archivo .jpg valido";
System.IO.File.Delete(Server.MapPath(sSavePath + sFilename));
}
}
}
在用户完成配置文件的其他字段(姓名、电子邮件等)后,会出现一个保存按钮,并使用此按钮保存到数据库中:
这是前面的密码
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pruebaConnectionString %>"
InsertCommand="INSERT INTO Curriculum(Nombre, Correo) VALUES (@TextBox1, @TextBox2)">
<InsertParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="" Name="TextBox1" PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" DefaultValue="" Name="TextBox2" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
现在我要做的是,当用户单击save按钮(或者方法按钮上的内容)时,图像url被保存到Imagen字段上的数据库中,Imagen字段是一个varchar 50,但不起作用,我得到:无法将值NULL插入到'Nombre'列中,表'prueba.dbo.currency';列不允许空值。插入失败。
声明已终止
但是如果我只使用SqlDataSource1.Insert()保留button1\u-click方法;这些字段将保存到数据库中
知道如何将图像url保存到数据库中吗?希望我的解释清楚
谢谢D您是在尝试创建新记录(您需要在其中指定所有值)还是尝试更新记录 目前,您的代码正在尝试执行插入操作,这将在课程表中创建一条新记录,但您只设置了1值。也许你想做一个更新
string strQuery = "UPDATE curriculum SET imagen = @imgPicture WHERE Nombre = ???";
你完全正确!!这是更新!!但是nombre?现在起作用的是什么呢?我写了这个字符串strQuery=“UPDATE-courseum-SET-imagen=@imgPicture”;但是更新了所有的记录,不仅仅是我插入的记录你需要替换吗???使用刚才插入的nombre值,可能是TextBox1.Text?
string strQuery = "UPDATE curriculum SET imagen = @imgPicture WHERE Nombre = ???";