我在做一个用户配置文件,用户首先选择的图片,并上传到这个代码的文件夹,之后显示的图像上传:图像路径到SQL数据库
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>
其实有铁道部Ë领域,而是使之短,我只是复制第2,农布雷场是唯一不能为空数据库
代码背后:
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.Insert();
String strConn = "Data Source=TOSHI;Initial Catalog=prueba;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string strQuery = "Insert into curriculum (imagen) values (@imgPicture)";
cmd.CommandText = strQuery;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@imgPicture", (imgPicture.ImageUrl == null ? (object)DBNull.Value : (object)imgPicture.ImageUrl));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
现在我想要做的是当用户点击保存按钮(或方法button1_click上的内容)时,图像url将保存到字段Imagen的数据库中,该字段是一个varchar 50,但不起作用,我得到:无法插入值NULL纳入'Nombre'列,'prueba.dbo.Curriculum'表;列不允许有空值。 INSERT失败。 该声明已被终止。
但是,如果我离开button1_click方法只有SqlDataSource1.Insert();这些字段被保存到数据库中。
任何想法如何将图像url保存到数据库?希望我对我的解释清楚!
谢谢! :D
你是完全正确的!它的更新!但是在哪里呢? – Ivelisse 2012-07-11 03:47:14
现在正在工作,我写了这个字符串strQuery =“更新课程设置imagen = @imgPicture”;但更新所有记录不仅是我插入的那个 – Ivelisse 2012-07-11 03:58:40
您需要更换???用刚刚插入的nombre的值,也许是TextBox1.Text? – Greg 2012-07-12 00:03:40