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,027

HOME > บทความจากสมาชิก > ใช้ ASP.Net ติดต่อ MS SQL Server ง่ายนิดเดียวด้วย Class SqlDatabaseManager



 
Clound SSD Virtual Server

ใช้ ASP.Net ติดต่อ MS SQL Server ง่ายนิดเดียวด้วย Class SqlDatabaseManager

SqlDatabaseManager Class
เชื่อว่าโปรแกรมเมอร์ทุกคน ต้องเคยทำงานที่เกี่ยวข้องกับฐานข้อมูลแน่ๆ ซึ่งในการติดต่อฐานข้อมูลแต่ละครั้ง
เราก็ต้องสร้าง Connection ต้องส่ง Command รวมถึง Parameter ต่างๆ ที่ต้องส่งไป แล้วยังต้องการห่วงเรื่อง Type เรื่อง Format อีกมากมาย
ซึ่งเป็นเรื่องมักเป็นปัญหากับโปรแกรมเมอร์มือใหม่ หรือมือเก่าแต่ขี้เกียจอย่างมาก ดังนั้นผมจึงได้เขียนคราสขึ้นมาคราสหนึ่ง
ที่จะทำให้การเขียนโปรแกรมติดต่อฐานข้อมูลเป็นเรื่องง่ายเหมือนกับการใช้ Command กับ MS SQL Server โดยตรง

แล้วมันเป็นอย่างไรล่ะ SqlDatabaseManager เนี่ย?
- มันก็เหมือนกับคราสอื่นๆ ของ ASP.Net แบบ OOP นี่แหละ ที่มี ConStruct มี Property มี Metrod ซึ่งเขียนตัว C#
จะใช้งานก็แค่ Copy ไปวางไว้ที่ Folder App_Code และเรียกใช้งานผ่าน Object แน่นั้นแหละ

อืม มันทำอะไรได้บ้างล่ะ?
- มันก็ทำได้เหมือนๆ กับ SQL ทำได้แหละ Select, Insert, Update, Delete ตามที่เราจะส่ง Command ไปให้มันทำงาน

อ้าว แล้วอย่างนี่มันต่างกับแบบปกติตรงไหน?
- เอาเป็นว่าแค่ส่ง Sql Command ให้มันแค่นี้ก็เสร็จเรียบร้อยแล้ว รอรับข้อมูลหรือผลลัพธ์ได้เลย

ว่าแต่โม้ไว้ซะเยอะ มันใช้งานอย่างไรล่ะ?
- เริ่มแรกนะ ก็ copy SqlDatabaseManager.cs เนี่ยไปวางไว้ที่ Folder App_Code จากนั้นก็สร้าง Object จาก Class SqlDatabaseManager แล้วก็ป้อน Command ให้มัน เสร็จแล้วสั่ง Execute แค่นี้ก็เสร็จแล้ว ดูจากตัวอย่างเลย

Code
DataTable Dt = new DataTable();

SqlDatabaseManager MyTest = new SqlDatabaseManager();
MyTest.CommandString = "Select * From [MyTable]";
Dt = MyTest.ExecuteQuery();


แค่นี้ก็เสร็จแล้ว สามารถเอา Dt ไปใช้งานได้อย่างสบายใจ

มาดูกันว่ามันมีอะไรให้เล่นอีกบาง โดยเริ่มจาก

ConStruct
SqlDatabaseManager() จะใช้ค่า Connection ที่ Config ไว้ที่ Web.Config โดยเรียกจากตัวแปรที่ชื่อว่า SqlConnectionString
SqlDatabaseManager(string SqlConnectionString) เป็นการกำหนด Connection เอง

Property
string ConnectionString ใช้กำหนดหรือเรียกดู SQL Connection
string CommandString ใช้กำหนดหรือเรียกดู SQL Command
bool IsSuccess ไว้สำหรับตรวจสอบว่า SQL Command ที่ส่งไปนั้นทำงานได้หรือไม่
string Message แสดงข้อความ บอกให้เราทราบว่า SQL Command ที่ส่งไปนั้นทำงานได้หรือไม่ ผิดพลาดอย่างไร

Metrod
void AddParameter(string ParameterName, SqlDbType ParameterType, string ParameterValue) กำหนด Type และ Value ให้ Parameter ใน SQL Command
void AddBinaryStream(string ParameterName, Stream BinaryStream, int StreamLength) ป้อน Value แบบ Binary Stream ให้ Parameter ใน SQL Command ในกรณีที่ต้องการเก็บ File ลงฐานข้อมูล
DataTable ExecuteQuery() คำสั่งรัน Query Command
object ExecuteScalar() คำสั่งรัน Query Command แบบ Scalar
bool ExecuteNonQuery() คำสั่งรัน Command แบบไม่มีการ Return ค่ากลับมา

ใน SqlDatabaseManager.cs ผมยังแถม Class JoinDataTable เอาไว้ให้สำหรับเชื่อมข้อมูล DataTable 2 DataTable ที่มีความสัมพันธ์เข้าด้วยกัน
ถ้าถามว่าใช้ Sql Command Query เอาไม่ง่ายกว่ารึ ต้องขอบอกว่าถ้าข้อมูลใน DataTable ที่เราได้มาไม่ได้มาจากฐานข้อมูลล่ะ
อย่างได้มาจาก Web Service ล่ะ เราก็ต้องใช้ Class นี่แหละ

ตัวอย่างวิธีการใช้งาน

Datetime
DataTable Dt = new DataTable();

SqlDatabaseManager MyTest = new SqlDatabaseManager();

MyTest.CommandString = "Select * From [MyFiend] Where [Birthday] Between SmallDateTime(@StartDate) And SmallDateTime(@EndDate)";
MyTest.AddParameter("@StartDate", SqlDbType.SmallDateTime, "1/10/2009");
MyTest.AddParameter("@EndDate", SqlDbType.SmallDateTime, "31/10/2009");

Dt = MyTest.ExecuteQuery();


เราจะไม่ต้องกังวลกับ Format ของวันที่อีกต่อไปเพียงใช้ command SmallDateTime(ตัวแปร) หรือ DateTime(ตัวแปร)
ตาม Type ในฐานข้อมูลของเรา มันก็จะทำให้ MS SQL Server รู้จัก Format วันที่ของเราเอง (เป็น Native SQL Command ของผมเอง)

ExecuteScalar
SqlDatabaseManager MyTest = new SqlDatabaseManager();

MyTest.CommandString = "Select Count(*) From [MyFiend] Where [Birthday] Between DateTime(@StartDate) And DateTime(@EndDate)";
MyTest.AddParameter("@StartDate", SqlDbType.DateTime, "1/10/2009");
MyTest.AddParameter("@EndDate", SqlDbType.DateTime, "31/10/2009");

Label1.Text = MyTest.ExecuteScalar().ToString();


ExecuteNonQuery & Store File Into Database
SqlDatabaseManager UpFile = new SqlDatabaseManager();

UpFile.CommandString = "Insert Into [UploadFile] ([FileName], [ContentType], [BinaryStream]) Values (@FileName, @ContentType, @BinaryStream)";
UpFile.AddParameter("@FileName", SqlDbType.NVarChar, FileUpload1.PostedFile.FileName);
UpFile.AddParameter("@ContentType", SqlDbType.NVarChar, FileUpload1.PostedFile.ContentType);
UpFile.AddBinaryStream("@BinaryStream", FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.ContentLength);

UpFile.ExecuteNonQuery();



********************************************* ของใหม่ ***************************************************

มี version ใหม่แล้วนะ แต่ยังไม่ได้เอาไป update ใน community (เพราะโรคเดิมๆ อู้ไม่เลิก)

มีอะไรใน vision ใหม่

- สามารถใช้ transaction ได้
- add parameter โดยไม่ต้อง กำหนด SqlDataType
- add parameter ได้ทุก type โดยต้องกำหนด type ให้ตรงกับใน Sql Server (สามารถเทียบระหว่าง .Net Type กับ Sql Type ได้จาก Mapping CLR Parameter Data)
- เรียก connection string จาก web.config ด้วย keyname ได้
- เมื่อ command success สามารถบอกจำนวนของ row ที่ทำการ insert update delete ได้
- ยกเลิก method AddBinaryStream() โดยเปลี่ยนไปใช้ SqlConvert class แทน
- สามารถกำหนด format ของ datetime type ตามความต้องการได้ (ด้วย SqlConvert class)
- ลดลูปในการทำงาน ทำให้ทำงานเร็วขึ้น
- คืน memmory เมื่อเสร็จสิ้นการทำงาน

SqlDatabaseManager Class








#Constructor
- public SqlDatabaseManager() จะใช้ค่า Connection ที่ Config ไว้ที่ Web.Config โดยเรียกจาก KeyName ที่ชื่อว่า SqlConnectionString
- public SqlDatabaseManager(string SqlConnectionKeyName) จะใช้ค่า Connection ที่ Config ไว้ที่ Web.Config โดยเรียกจาก KeyName ที่กำหนดเอง
- public SqlDatabaseManager(string SqlConnectionKeyName, string SqlConnectionString) กำหนดค่า KeyName และ Connection String เอง

#Property
- public virtual string ConnectionKeyName กำหนดและเรียกดูค่า Connection KeyName
- public virtual string ConnectionString กำหนดและเรียกดูค่า Connection String
- public virtual string CommandString กำหนดและเรียกดูค่า Sql Command String
- public virtual bool IsSuccess เรียกดูผลของ Sql Command
- public virtual string Message เรียกดูคำอธิบายผลของ Sql Command
- public virtual int RowsAffected เรียกดูจำนวน Row ที่ทำการ ExecuteNonQuery ไป

#Method
- public virtual void AddParameter(string ParameterName, object ParameterValue) กำหนดชื่อ parameter และป้อน value เพื่อแทนค่าใน Sql Command
- public virtual void TransactionStart() เริ่มต้นการใช้งาน Transaction
- public virtual bool ExecuteTransaction() คำสั่งสำหรับรัน Transaction
- public virtual DataTable ExecuteQuery() คำสั่งรัน Query Command
- public virtual object ExecuteScalar() คำสั่งรัน Query Command แบบ Scalar
- public virtual bool ExecuteNonQuery() คำสั่งรัน Command แบบไม่มีการ Return ค่ากลับมา

SqlConvert Class Class ที่ต้องใช้งานร่วมกัน

#Method
- public static byte[] ToVarBinary(Stream BinaryStream, int StreamLength) แปลง BanaryStream (จาก FileUpload Control) เป็น byte[] เพื่อเก็บลง Sql Server
- public static DateTime ToDateTime(string DateString) แปลง String เป็น DateTime Type ที่มี Format 'd/M/yyyy'
- public static DateTime ToDateTime(string DateString, string DateFormat) แปลง String เป็น DateTime Type ที่มี Format ที่กำหนดเอง



Basic Query แบบบ้านๆ

Code (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestSqlConnection.aspx.cs" Inherits="TestSqlConnection" %>

<!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:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

public partial class TestSqlConnection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDatabaseManager SqlDatabaseManager1 = new SqlDatabaseManager();
        SqlDatabaseManager1.CommandString = "Select [DayID], [DayName] From [DayTable]";

        DataTable Dt = new DataTable();
        Dt = SqlDatabaseManager1.ExecuteQuery();

        GridView1.DataSource = Dt;
        GridView1.DataBind();

        Label1.Text = SqlDatabaseManager1.Message;
    }
}


ผลลัพธ์
1



Query แบบมีเงื่อนไขนิดๆ

Code (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestSqlConnection.aspx.cs" Inherits="TestSqlConnection" %>

<!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:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

public partial class TestSqlConnection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDatabaseManager SqlDatabaseManager1 = new SqlDatabaseManager();
        SqlDatabaseManager1.CommandString = "Select [DayID], [DayName] From [DayTable] Where [DayName] Like @DayName Or [DayID]=@DayID";
        SqlDatabaseManager1.AddParameter("@DayName", "Wed%");
        SqlDatabaseManager1.AddParameter("@DayID", 2);

        DataTable Dt = new DataTable();
        Dt = SqlDatabaseManager1.ExecuteQuery();

        GridView1.DataSource = Dt;
        GridView1.DataBind();

        Label1.Text = SqlDatabaseManager1.Message;
    }
}


ผลลัพธ์
2



Query โดยใช้ Datetime แบบ String (ใช้ native command 'SmallDateTime()' หรือ 'DateTime()' ช่วย)

Code (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestSqlConnection.aspx.cs" Inherits="TestSqlConnection" %>

<!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:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

public partial class TestSqlConnection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDatabaseManager SqlDatabaseManager1 = new SqlDatabaseManager();
        SqlDatabaseManager1.CommandString = "Select [HolidayTable].[HolidayID], [DayTable].[DayName], [HolidayTable].[Holiday], [HolidayTable].[HolidayThaiDetail], [HolidayTable].[HolidayDetail] From [HolidayTable] Inner Join [DayTable] On ([HolidayTable].[DayID] = [DayTable].[DayID]) Where [HolidayTable].[Holiday]=SmallDateTime(@MyDate)";
        SqlDatabaseManager1.AddParameter("@MyDate", "13/4/2010");

        DataTable Dt = new DataTable();
        Dt = SqlDatabaseManager1.ExecuteQuery();

        GridView1.DataSource = Dt;
        GridView1.DataBind();

        Label1.Text = SqlDatabaseManager1.Message;
    }
}


ผลลัพธ์
3



Query โดยใช้ Datetime แบบ DateTime Type (แนะนำ)

Code (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestSqlConnection.aspx.cs" Inherits="TestSqlConnection" %>

<!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:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

public partial class TestSqlConnection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDatabaseManager SqlDatabaseManager1 = new SqlDatabaseManager();
        SqlDatabaseManager1.CommandString = "Select [HolidayTable].[HolidayID], [DayTable].[DayName], [HolidayTable].[Holiday], [HolidayTable].[HolidayThaiDetail], [HolidayTable].[HolidayDetail] From [HolidayTable] Inner Join [DayTable] On ([HolidayTable].[DayID] = [DayTable].[DayID]) Where [HolidayTable].[Holiday]=@MyDate";
        SqlDatabaseManager1.AddParameter("@MyDate", SqlConvert.ToDateTime("13/4/2010"));

        DataTable Dt = new DataTable();
        Dt = SqlDatabaseManager1.ExecuteQuery();

        GridView1.DataSource = Dt;
        GridView1.DataBind();

        Label1.Text = SqlDatabaseManager1.Message;
    }
}


ผลลัพธ์
3








การใช้ ExecuteScalar()

Code (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestSqlConnection.aspx.cs" Inherits="TestSqlConnection" %>

<!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:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TestSqlConnection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDatabaseManager SqlDatabaseManager1 = new SqlDatabaseManager();
        SqlDatabaseManager1.CommandString = "Select Count(*) From [HolidayTable]";

        int AllHoliday = (int)SqlDatabaseManager1.ExecuteScalar();

        Label1.Text = SqlDatabaseManager1.Message;
        Label2.Text = "All Holiday in 2010: " + AllHoliday + " Days";
    }
}


ผลลัพธ์
4



การใช้งาน ExecuteNonQuery() เพื่อ upload ไฟล์ไปเก็บไว้ใน Sql Server

Code (C#)
<%@ 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>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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)
    {
        SqlDatabaseManager SqlDatabaseManager1 = new SqlDatabaseManager();
        SqlDatabaseManager1.CommandString = "Insert Into [UploadFile] ([FileName], [ContentType], [BinaryStream]) Values (@FileName, @ContentType, @BinaryStream)";
        SqlDatabaseManager1.AddParameter("@FileName", FileUpload1.PostedFile.FileName);
        SqlDatabaseManager1.AddParameter("@ContentType", FileUpload1.PostedFile.ContentType);
        SqlDatabaseManager1.AddParameter("@BinaryStream", SqlConvert.ToVarBinary(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.ContentLength));
        SqlDatabaseManager1.ExecuteNonQuery();

        Label1.Text = SqlDatabaseManager1.Message;
        Label2.Text = "Insert " + SqlDatabaseManager1.RowsAffected + " Row(s).";
    }
}


ผลลัพธ์
5



การใช้งาน transaction ทำการ upload ไฟล์ไปเก็บไว้ใน Sql Server และเก็บวันที่ upload ไว้อีก table นึง

Code (C#)
<%@ 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>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Code (C#)
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)
    {
        SqlDatabaseManager SqlDatabaseManager1 = new SqlDatabaseManager();

        SqlDatabaseManager1.TransactionStart();
        //ตรวจสอบก่อน execute
        Label1.Text = SqlDatabaseManager1.Message; 

        SqlDatabaseManager1.CommandString = "Insert Into [UploadFile] ([FileName], [ContentType], [BinaryStream]) Values (@FileName, @ContentType, @BinaryStream)";
        SqlDatabaseManager1.AddParameter("@FileName", FileUpload1.PostedFile.FileName);
        SqlDatabaseManager1.AddParameter("@ContentType", FileUpload1.PostedFile.ContentType);
        SqlDatabaseManager1.AddParameter("@BinaryStream", SqlConvert.ToVarBinary(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.ContentLength));
        SqlDatabaseManager1.ExecuteNonQuery();
        //ตรวจสอบตอนรัน command แรก
        Label2.Text = SqlDatabaseManager1.Message;

        //สามารถ query ที่ insert ภายใน transaction ได้
        SqlDatabaseManager1.CommandString = "Select Max(ID) From [UploadFile]";
        int MaxID = (int)SqlDatabaseManager1.ExecuteScalar();

        SqlDatabaseManager1.CommandString = "Insert Into [UploadDetail] ([FileID], [UploadDate]) Value (@FileID, @UploadDate)";
        SqlDatabaseManager1.AddParameter("@FileID", MaxID);
        SqlDatabaseManager1.AddParameter("@UploadDate", DateTime.Today);
        SqlDatabaseManager1.ExecuteNonQuery();
        //ตรวจสอบตอนรัน command ที่สาม
        Label3.Text = SqlDatabaseManager1.Message;

        SqlDatabaseManager1.ExecuteTransaction();
        //ผลของ transaction
        Label4.Text = SqlDatabaseManager1.Message;
    }
}


ผลลัพธ์แบบ error เพราะอยากให้ดูแบบ error เพราะ value ไม่ได้เติม s
6

ผลลัพธ์แบบ success แก้ไข values แล้ว
7



   
Share
Bookmark.   

  By : xxx
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2009-12-18
  Download : Download  ใช้ ASP.Net ติดต่อ MS SQL Server ง่ายนิดเดียวด้วย Class SqlDatabaseManager (0.0000 MB)
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
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 อัตราราคา คลิกที่นี่