Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,030

HOME > .NET Framework > Forum > รบกวนดูหน่อยคับ .. nvarchar is incompatible with image



 

รบกวนดูหน่อยคับ .. nvarchar is incompatible with image

 



Topic : 040281

Guest




asp


พอกด ใส่รูป ขึ้นแบบนี้อะคับ



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-03-13 17:18:52 By : Ping View : 1177 Reply : 2
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

nvarchar is incompatible with image






Date : 2010-03-14 20:04:10 By : webmaster
 


 

No. 2



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ใช่คนเดียวกับด้านล่างหรือเปล่า จะตั้งทำไมเยอะนัก

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
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : รบกวนดูหน่อยคับ .. nvarchar is incompatible with image
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่