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 > .NET Framework > Forum > C# ช่วยด้วยคับ คือผมบันทึกข้อมูลลงไปใน Sql Server ไม่ได้คับ ช่วยผมหน่อยคับ



 

C# ช่วยด้วยคับ คือผมบันทึกข้อมูลลงไปใน Sql Server ไม่ได้คับ ช่วยผมหน่อยคับ

 



Topic : 098520



โพสกระทู้ ( 37 )
บทความ ( 0 )



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




คือบันทึกครั้งแรกผมบันทึกได้คับ พอจะบันทึกอีกครั้งมันไม่ได้อ่ะคับ

Screenshot

Code (C#)
private void Save()
        {
            Connect objconn = new Connect();
            SqlCommand sqlCMD;
            SqlDataReader sqlDR;
            //DataTable DT;

            objconn.Conn();
            {
                string strSQL;
                strSQL = "Select * from Table_Agent order by Agent_ID";
                sqlCMD = new SqlCommand(strSQL, objconn.conns);
                sqlDR = sqlCMD.ExecuteReader();
                if (sqlDR.Read())
                {
                    strSQL = "Update Table_Agent set Agent_Name = '" + textBox10.Text +"'";
                    strSQL += "Agent_Store = '" + textBox9.Text + "'";
                    strSQL += "Agent_HouseNo = '" + textBox3.Text + "'";
                    strSQL += "Agent_Village = '" + textBox4.Text + "'";
                    strSQL += "Agent_District = '" + textBox5.Text + "'";
                    strSQL += "Agent_Prefecture = '" + textBox6.Text + "'";
                    strSQL += "Agent_Province = '" + textBox7.Text + "'";
                    strSQL += "Agent_PostalCode = '" + maskedTextBox1.Text + "'";
                    strSQL += "Agent_Phone = '" + maskedTextBox2.Text + "'";
                    strSQL += "where Agent_ID = '" + textBox1.Text + "'";
                }
                else
                {
                    strSQL = "Insert into Table_Agent(Agent_ID,Agent_Name,Agent_Store,Agent_HouseNo, Agent_Village,Agent_District,Agent_Prefecture,Agent_Province, Agent_PostalCode,Agent_Phone)";
                    strSQL += "Values('" + textBox1.Text + "' ,'" + textBox10.Text + "', '" + textBox9.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "' ,'" + textBox7.Text + "','" + maskedTextBox1.Text + "','" + maskedTextBox2.Text + "')";
                }
                sqlDR.Close();
                sqlCMD = new SqlCommand(strSQL, objconn.conns);
                int numRec = sqlCMD.ExecuteNonQuery();
                if (numRec > 0)
                {
                    MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว");
                }
                else
                {
                    MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้");
                }
            }
        }

private void button12_Click(object sender, EventArgs e)
        {
            DialogResult strDel;
            strDel = MessageBox.Show("คุณต้องการลบข้อมมูลใช่หรือไม่ ?", "ยืนยันการลบ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (strDel == DialogResult.OK)
            {
                Dele();
            }
            showAgent();
        }




Tag : Ms SQL Server 2012, C#, C







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-07-31 13:18:22 By : coutiho View : 2835 Reply : 24
 

 

No. 1



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

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

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

Quote:
strSQL = "Update Table_Agent set Agent_Name = '" + textBox10.Text +"'";
strSQL += "Agent_Store = '" + textBox9.Text + "'";
strSQL += "Agent_HouseNo = '" + textBox3.Text + "'";
strSQL += "Agent_Village = '" + textBox4.Text + "'";
strSQL += "Agent_District = '" + textBox5.Text + "'";
strSQL += "Agent_Prefecture = '" + textBox6.Text + "'";
strSQL += "Agent_Province = '" + textBox7.Text + "'";
strSQL += "Agent_PostalCode = '" + maskedTextBox1.Text + "'";
strSQL += "Agent_Phone = '" + maskedTextBox2.Text + "'";
strSQL += "where Agent_ID = '" + textBox1.Text + "'";


พวก Comma (,) หายไปไหนหมดครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-07-31 20:58:37 By : mr.win
 


 

No. 2



โพสกระทู้ ( 697 )
บทความ ( 0 )



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

,


ประวัติการแก้ไข
2013-08-01 00:21:06
2013-08-01 00:21:26
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-01 00:20:36 By : 01000010
 

 

No. 3



โพสกระทู้ ( 37 )
บทความ ( 0 )



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


เอาใส่ตรงไหนหรอคับ ยัง งงๆอยู่เลยคับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-01 20:56:28 By : coutiho
 


 

No. 4



โพสกระทู้ ( 697 )
บทความ ( 0 )



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

ตอบความคิดเห็นที่ : 3 เขียนโดย : coutiho เมื่อวันที่ 2013-08-01 20:56:28
รายละเอียดของการตอบ ::
ขี้เกียจอธิบายครับ ลองไปดูตัวอย่างในเว็บครับ สังเกตุดีๆ ว่ามันใส่ยังไง แล้วนำมาประยุกต์กับคำสั่งที่เราเชื่อมข้อความครับ

http://www.w3schools.com/sql/sql_update.asp

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-01 21:13:42 By : 01000010
 


 

No. 5



โพสกระทู้ ( 21 )
บทความ ( 0 )



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


debug แล้วเอา code ออกมาดูครับ ตรง Update เครื่องหมาย (,) ขั้นระหว่าง column หายไปหมดเลย
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-01 21:39:20 By : mungkorn001
 


 

No. 6



โพสกระทู้ ( 37 )
บทความ ( 0 )



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


กระจางแล้วคับ ขอบคุณมากคับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-02 07:59:52 By : coutiho
 


 

No. 7



โพสกระทู้ ( 37 )
บทความ ( 0 )



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


คือใส่แล้วคับ มันบอกว่า "ไม่สามารถบันทึกข้อมูลได้" คับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-04 11:00:38 By : coutiho
 


 

No. 8



โพสกระทู้ ( 697 )
บทความ ( 0 )



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

ตอบความคิดเห็นที่ : 7 เขียนโดย : coutiho เมื่อวันที่ 2013-08-04 11:00:38
รายละเอียดของการตอบ ::
สำคัญที่ตัวแปร numRec มันเก็บค่า ที่ถูกคืนค่าจะการ Update หรือ Insert

ผมอาจจะไม่เข้าใจโปรแกรมคุณ (ฝีมือยังไม่ถึง) แต่ผมรู้สึกว่าอัลกอริทึมมันแปลกๆนะ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-04 14:40:18 By : 01000010
 


 

No. 9



โพสกระทู้ ( 35 )
บทความ ( 0 )



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


Code (C#)
strSQL = "Update Table_Agent set Agent_Name = '" + textBox10.Text +"',";
strSQL += "Agent_Store = '" + textBox9.Text + "',";
strSQL += "Agent_HouseNo = '" + textBox3.Text + "',";
strSQL += "Agent_Village = '" + textBox4.Text + "',";
strSQL += "Agent_District = '" + textBox5.Text + "',";
strSQL += "Agent_Prefecture = '" + textBox6.Text + "',";
strSQL += "Agent_Province = '" + textBox7.Text + "',";
strSQL += "Agent_PostalCode = '" + maskedTextBox1.Text + "',";
strSQL += "Agent_Phone = '" + maskedTextBox2.Text + "',";


แถวสุดท้ายนี้ไม่ใส่ (,) น่ะคับ

Code (C#)
strSQL += "where Agent_ID = '" + textBox1.Text + "'";

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-04 19:17:41 By : keooudone
 


 

No. 10



โพสกระทู้ ( 37 )
บทความ ( 0 )



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


Code (C#)
private void Save()
        {
            Connect objconn = new Connect();
            SqlCommand sqlCMD;
            SqlDataReader sqlDR;
            //DataTable DT;

            objconn.Conn();
            {
                string strSQL;
                strSQL = "Select * from Table_Agent order by Agent_ID";
                sqlCMD = new SqlCommand(strSQL, objconn.conns);
                sqlDR = sqlCMD.ExecuteReader();
                if (sqlDR.Read())
                {
                    strSQL = "Update Table_Agent set Agent_Name = '" + textBox10.Text +"'";
                    strSQL += ",Agent_Store = '" + textBox9.Text + "'";
                    strSQL += ",Agent_HouseNo = '" + textBox3.Text + "'";
                    strSQL += ",Agent_Village = '" + textBox4.Text + "'";
                    strSQL += ",Agent_District = '" + textBox5.Text + "'";
                    strSQL += ",Agent_Prefecture = '" + textBox6.Text + "'";
                    strSQL += ",Agent_Province = '" + textBox7.Text + "'";
                    strSQL += ",Agent_PostalCode = '" + maskedTextBox1.Text + "'";
                    strSQL += ",Agent_Phone = '" + maskedTextBox2.Text + "'";
                    strSQL += "where Agent_ID = '" + textBox1.Text + "'";
                }
                else
                {
                    strSQL = "Insert into Table_Agent(Agent_ID,Agent_Name,Agent_Store,Agent_HouseNo, Agent_Village,Agent_District,Agent_Prefecture,Agent_Province, Agent_PostalCode,Agent_Phone)";
                    strSQL += "Values('" + textBox1.Text + "' ,'" + textBox10.Text + "', '" + textBox9.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "' ,'" + textBox7.Text + "','" + maskedTextBox1.Text + "','" + maskedTextBox2.Text + "')";
                }
                sqlDR.Close();
                sqlCMD = new SqlCommand(strSQL, objconn.conns);
                int numRec = sqlCMD.ExecuteNonQuery();
                if (numRec > 0)
                {
                    MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว");
                }
                else
                {
                    MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้");
                }
            }
        }

private void button12_Click(object sender, EventArgs e)
        {
            DialogResult strDel;
            strDel = MessageBox.Show("คุณต้องการลบข้อมมูลใช่หรือไม่ ?", "ยืนยันการลบ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (strDel == DialogResult.OK)
            {
                Dele();
            }
            showAgent();
        }



แต่มันก็ไม่ได้อ่ะคับ ปวดหัวมากๆๆๆๆๆๆๆๆๆๆๆๆๆๆ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-07 10:51:19 By : coutiho
 


 

No. 11



โพสกระทู้ ( 697 )
บทความ ( 0 )



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

ผมงงตั้งแต่บรรทัดนี้แล้วครับ

Code (C#)
objconn.Conn();
{



แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-07 11:11:50 By : 01000010
 


 

No. 12



โพสกระทู้ ( 257 )
บทความ ( 0 )



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


Code (C#)
objconn.Conn();
{


งง ตรงนี้เหมือนกันครับ

ว่าแต่สรุปแล้ว จะทำอะไรในลักษณะไหนครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-07 12:11:44 By : CPU4Core
 


 

No. 13



โพสกระทู้ ( 37 )
บทความ ( 0 )



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


ผมจะบันทึกข้อมูลลงฐานข้อมูล แล้วมันบันทึกไม่ได้คับ มันโชว์
MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้")
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-10 11:07:14 By : coutiho
 


 

No. 14



โพสกระทู้ ( 177 )
บทความ ( 0 )



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

คอมเม้นตรงนี้ไว้ก่อน

Code (C#)
if (numRec > 0)
{
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว");
}
else
{
MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้");
}


แล้วดูว่าโปรแกรม แสดง Error ว่าอะไร
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-08-15 11:37:17 By : bangbang111
 


 

No. 15



โพสกระทู้ ( 37 )
บทความ ( 0 )



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


ไม่ Error อะไรเลยคับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-14 19:11:05 By : coutiho
 


 

No. 16



โพสกระทู้ ( 213 )
บทความ ( 0 )

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

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


int numRec = sqlCMD.ExecuteNonQuery();
if (numRec > 0)

ตรงนี้ผมเข้าใจว่า ถ้า ExecuteNonQuery() สำเร็จ กำหนดค่าให้ numRec = 1
เพื่อให้ขึ้น PopUp เตือน
ลองเปลี่ยนดีไหมครับ จะได้รู้ว่า error ของระบบเป็นอะไร

Code (C#)
string errMsg = string.Empty;
bool success = false;
try 
{
     sqlCMD.ExecuteNonQuery();
     success = true;
}
catch (Exception ex) 
{
    errMsg = ex.Message;
}
finally
{
     if (success)
     {
          MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว");
     }
     else
     {
          MessageBox.Show(errMsg);
     }
}



ประวัติการแก้ไข
2013-09-14 19:58:55
2013-09-14 20:00:00
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-14 19:58:14 By : Aod47
 


 

No. 17



โพสกระทู้ ( 15 )
บทความ ( 0 )



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


Code (C#)
strSQL = "Select * from Table_Agent order by Agent_ID";   <<< 
sqlCMD = new SqlCommand(strSQL, objconn.conns);
sqlDR = sqlCMD.ExecuteReader();
if (sqlDR.Read()) 
{
    update
}
else
{
     insert
}


เมื่อไรจะเข้า else อะครับ หรือผมเข้าใจผิด
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-24 09:49:19 By : ghosting789
 


 

No. 18

Guest


รบกวนหน่อยน่ะคร้า ปุ่มบันทึกข้อมูลคร้า (มันบันทึกไม่ได้อ่ะคร้า ไม่ลงฐานข้อมูล)



Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        SqlConnection Conn;

        SqlCommand Com;
        SqlDataReader dr;

       

        StringBuilder sb = new StringBuilder();

        private void Form1_Load(object sender, EventArgs e)
        {

        string strConn;

        strConn = "Data Source=./SQLEXPRESS; Initial Catalog=lam; Integrated Security=True";

        Conn = new SqlConnection();

        if (Conn.State == ConnectionState.Open)

        {

            Conn.Close();

        }

           Conn.ConnectionString = strConn;

           Conn.Open();

      }

        private void button2_Click(object sender, EventArgs e)
        {

            if (tetID.Text.Trim() == "")
            {

                MessageBox.Show("กรุณาป้อนรหัสลูกค้า !!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                tetID.Focus();
                return;

             }
            sb.Remove(0, sb.Length);

            sb.Append("INSERT INTO lam(ID,name,lastname)VALUES(@txtID,@txtname,@tetlastname)");

            sb.Append(" VALUES (@ma,@lamDataSet1)");

            string sqlSave = sb.ToString();

            Com.CommandType = CommandType.Text;
            Com.CommandType = CommandType.Text;
            Com.CommandType = CommandType.Text;
            Com.CommandType = CommandType.Text;
            Com.CommandText = sqlSave;
            Com.Connection = Conn;

           
            Com.Parameters.Clear();
            Com.Parameters.Add("@txtID", SqlDbType.NChar).Value = tetID.Text.Trim();
            Com.Parameters.Add("@txtname", SqlDbType.NChar).Value = tetname.Text.Trim();
            Com.Parameters.Add("@lastname", SqlDbType.NChar).Value = tetlastname.Text.Trim();
            Com.ExecuteNonQuery();
            MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "รายงานผล", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
     

       
    }

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-11-21 14:29:29 By : ด้าดา
 


 

No. 19

Guest


รบกวนหน่อยน่ะคร้า ปุ่มบันทึกข้อมูลคร้า (มันบันทึกไม่ได้อ่ะคร้า ไม่ลงฐานข้อมูล)



Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        SqlConnection Conn;

        SqlCommand Com;
        SqlDataReader dr;

       

        StringBuilder sb = new StringBuilder();

        private void Form1_Load(object sender, EventArgs e)
        {

        string strConn;

        strConn = "Data Source=./SQLEXPRESS; Initial Catalog=lam; Integrated Security=True";

        Conn = new SqlConnection();

        if (Conn.State == ConnectionState.Open)

        {

            Conn.Close();

        }

           Conn.ConnectionString = strConn;

           Conn.Open();

      }

        private void button2_Click(object sender, EventArgs e)
        {

            if (tetID.Text.Trim() == "")
            {

                MessageBox.Show("กรุณาป้อนรหัสลูกค้า !!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                tetID.Focus();
                return;

             }
            sb.Remove(0, sb.Length);

            sb.Append("INSERT INTO lam(ID,name,lastname)VALUES(@txtID,@txtname,@tetlastname)");

            sb.Append(" VALUES (@ma,@lamDataSet1)");

            string sqlSave = sb.ToString();

            Com.CommandType = CommandType.Text;
            Com.CommandType = CommandType.Text;
            Com.CommandType = CommandType.Text;
            Com.CommandType = CommandType.Text;
            Com.CommandText = sqlSave;
            Com.Connection = Conn;

           
            Com.Parameters.Clear();
            Com.Parameters.Add("@txtID", SqlDbType.NChar).Value = tetID.Text.Trim();
            Com.Parameters.Add("@txtname", SqlDbType.NChar).Value = tetname.Text.Trim();
            Com.Parameters.Add("@lastname", SqlDbType.NChar).Value = tetlastname.Text.Trim();
            Com.ExecuteNonQuery();
            MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "รายงานผล", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
     

       
    }

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-11-21 14:29:55 By : ด้าดา
 


 

No. 20

Guest


ลองดูตรงนี้ก่อน
ปุ่มบันทึกข้อมูล

ถ้าไม่ได้ ผมลองเอาโค๊ดเขียนในแบบผมดู

Code (C#)
        
        SqlConnection Conn = new sqlconnection("Data Source=./SQLEXPRESS; Initial Catalog=lam; Integrated Security=True");

        private void Form1_Load(object sender, EventArgs e)
        {


        if (Conn.State == ConnectionState.Open)

        {

            Conn.Close();

        }

      }

        private void button2_Click(object sender, EventArgs e)
        {

            if (tetID.Text.Trim() == "")
            {

                MessageBox.Show("กรุณาป้อนรหัสลูกค้า !!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                tetID.Focus();
                return;

             }
            try
			{
			conn.open();
			string sql = "INSERT INTO lam(ID,name,lastname)VALUES(@txtID,@txtname,@tetlastname)";
            sqlcommand cmd = new sqlcommand (sql,conn)
			string texID = tetID.Text.Trim();
			string texName = tetname.Text.Trim();
			string texLastName = tetlastname.Text.Trim();
			cmd.Parameters.Add(new SqlParameter("@txtID", texID));
			cmd.Parameters.Add(new SqlParameter("@txtname", texName));
			cmd.Parameters.Add(new SqlParameter("@txtlastname", texLastName));
			SqlDataReader reader = cmd.ExecuteReader();

            MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "รายงานผล", MessageBoxButtons.OK, MessageBoxIcon.Information);
			conn.close();
			}
			catch
			{
			MessageBox.show ("Error");
			}
        }

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-11-25 00:59:55 By : Laser.Wz
 


 

No. 21

Guest


ขอบคุณมากๆเลยค่ะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-11-25 11:41:08 By : ด้าดา
 


 

No. 22

Guest



รบกวนอีกทีน่ะ ค่ะ มัน Error ตรง SqlConnection conn; น่ะค่ะ แก้แล้วก็เหมือนเดิม ช่วยหน่อยน่ะค่ะ ขอบคุณมากค่ะ

SqlConnection conn;



StringBuilder sb = new StringBuilder();


SqlConnection Conn = new SqlConnection ("Data Source=./SQLEXPRESS; Initial Catalog=lam; Integrated Security=True");


private void Form1_Load(object sender, EventArgs e)
{

if (conn.State == ConnectionState.Open)
{

conn.Close();

}

}

private void button2_Click(object sender, EventArgs e)
{

if (tetID.Text.Trim() == "")

{

MessageBox.Show("กรุณาป้อนรหัสลูกค้า !!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );

tetID.Focus();

return;

}

try

{

conn.Open();

string sql = "INSERT INTO lam (ID,name,lastname)VALUES(@tetID,@tetname,@tetlastname)";

SqlCommand cmd = new SqlCommand(sql, conn);

string texID = tetID.Text.Trim();

string texName = tetname.Text.Trim();

string texlastName = tetlastname.Text.Trim();

cmd.Parameters.Add(new SqlParameter("@tetID", tetID));

cmd.Parameters.Add(new SqlParameter("@tetname", tetname));

cmd.Parameters.Add(new SqlParameter("@tetlastname", tetlastname));

SqlDataReader reader = cmd.ExecuteReader();

MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "รายงานผล", MessageBoxButtons.OK, MessageBoxIcon.Information);

Conn.Close();

}

catch

{

MessageBox.Show ("Error");

}

}
}
}
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-11-26 10:09:40 By : ด้าดา
 


 

No. 23

Guest



ทำได้แล้วน่ะค่ะ ลงฐานข้อมูลแล้วค่ะ ขอบคุณมากๆน่ะค่ะ (โอกาศหน้าจะมารบกวนอีกน่ะค่ะ)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-11-26 12:32:24 By : ด้าดา
 


 

No. 24

Guest



แก้ไขไม่ได้อ่ะค่ะ รบกวนช่วยดูให้หน่อยนะค่ะ ขอบคุณมากค่ะ :l

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SqlConnection conn;
SqlCommand comm;



private void Form1_Load(object sender, EventArgs e)
{

}


private void button1_Click(object sender, EventArgs e)
{


long Affect = 0; // ประกาศตัวแปรในการเช็คว่ามีข้อมูลหรือไม่ หรือ จะประยุกต์ เป็นการแบบ บูลีน ก้ได้ แล้ว Return ค่ามาค่ะ

string sqlEdit;

//SqlCommand comEdit = new SqlCommand();

if (MessageBox.Show("คุณต้องการแก้ไขข้อมูลสมาชิก ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{

sqlEdit = "UPDATE lam ";

sqlEdit += "ID='" + tetID.Text + "',";
sqlEdit += " name='" + tetname.Text + "',";
sqlEdit += " lastname='" + tetlastname.Text + "',";

try
{

comm.CommandType = CommandType.Text;
comm.CommandText = sqlEdit;
comm.Connection = conn;
Affect = comm.ExecuteNonQuery();

}

catch (Exception errToEdit)
{

MessageBox.Show("ไม่สามารถแก้ไขได้ เนื่องจาก " + errToEdit.Message, "เกิดข้อผิดพลาด");

}

if (Affect == 0) //กรณีไม่พบ
{

//MessageBox.Show("ไม่พบข้อมูล หรือ ไม่มีข้อมูลในระบบ", "ข้อความจากระบบ");

}

if (Affect > 0) //กรณีพบ
{

tetID.Text = "";
tetname.Text = "";
tetlastname.Text = "";

MessageBox.Show("แก้ไขข้อมูลพนักงาน เรียบร้อยแล้ว !!!", "ผลการทำงาน");


}
}
}
}
}
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-12-02 10:47:36 By : ด้าดา
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# ช่วยด้วยคับ คือผมบันทึกข้อมูลลงไปใน Sql Server ไม่ได้คับ ช่วยผมหน่อยคับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 05
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 อัตราราคา คลิกที่นี่