|
 |
|
รบกวนดูหน่อยคับ .. nvarchar is incompatible with image |
|
 |
|
|
 |
 |
|
nvarchar is incompatible with image
|
 |
 |
 |
 |
Date :
2010-03-14 20:04:10 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช่คนเดียวกับด้านล่างหรือเปล่า จะตั้งทำไมเยอะนัก
https://www.thaicreate.com/dotnet/forum/040211.html
https://www.thaicreate.com/dotnet/forum/040278.html
https://www.thaicreate.com/dotnet/forum/040280.html
จะทำอะไรกันแน่ครับ
จะ upload รูปลง web server หรือ
upload รูปลง database server
ถ้า upload รูปลง web server ก็เก็บเฉพาะ path ที่ up ใน server ลง db โดยใช้ datatype เป็น nvarchar
UploadFile.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFile.aspx.cs" Inherits="UploadFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
UploadFile.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
public partial class UploadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
}
protected void Button1_Click(object sender, EventArgs e)
{
string sqlConnectionString = WebConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
bool Success = false;
string SavePath = Server.MapPath("~/photo/picture/computer/");
if (FileUpload1.HasFile)
{
try
{
FileUpload1.SaveAs(SavePath + FileUpload1.FileName);
Success = true;
}
catch (Exception ex)
{
Label1.Text = ex.Message;
Success = false;
}
}
if (Success)
{
string SqlCommandString = "Insert Into [MyTable] ([PathFile]) Value (@PathFile)";
SqlCommand sqlCommand = new SqlCommand(SqlCommandString);
sqlCommand.Parameters.AddWithValue("@PathFile", SavePath + FileUpload1.FileName);
try
{
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
}
}
ถ้า upload รูปลง database server ก็เก็บชื่อไฟล์ (nvarchar) ไทป์ของไฟล์ (nverchar) และbinarydata (sql 2000 ใช้ image ส่วน sql 2005 ขึ้นไปใช้ varbinary)
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
}
protected void Button1_Click(object sender, EventArgs e)
{
string sqlConnectionString = WebConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
if (FileUpload1.HasFile)
{
BinaryReader BinaryRead = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] BinaryData = BinaryRead.ReadBytes(FileUpload1.PostedFile.ContentLength);
string sqlCommandString = "Insert Into [UploadFile] ([FileName], [ContentType], [BinaryStream]) Values (@FileName, @ContentType, @BinaryStream)";
SqlCommand sqlCommand = new SqlCommand(sqlCommandString);
sqlCommand.Parameters.AddWithValue("@FileName", FileUpload1.PostedFile.FileName);
sqlCommand.Parameters.AddWithValue("@ContentType", FileUpload1.PostedFile.ContentType);
sqlCommand.Parameters.AddWithValue(("@BinaryStream", BinaryData);
try
{
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
}
}
|
 |
 |
 |
 |
Date :
2010-03-15 09:22:06 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|