|
|
|
จะ Insert ข้อมูล ลง ตารางใน Database ผ่าน form ของ C# |
|
|
|
|
|
|
|
insert into TBStu( Fname ) values(@Fname)
ใช้รูปแบบนี้น่าจะดีกว่านะคะ
|
|
|
|
|
Date :
2011-09-28 10:18:16 |
By :
Tuckatan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดู Basic C# (Windows App) ครับ
Code (C#)
SqlCeConnection myConnection = default(SqlCeConnection);
//myConnection = new SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\\AppDatabase1.sdf;"));
myConnection = new SqlCeConnection("Data Source =C:\\WindowsFormsApplication\\WindowsFormsApplication\\Database1.sdf;");
myConnection.Open();
SqlCeCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "INSERT INTO [mytable] ([name], [email]) VALUES " + " ('" + this.txtName.Text + "','" + this.txtEmail.Text + "' ) ";
myCommand.CommandType = CommandType.Text;
myCommand.ExecuteNonQuery();
myConnection.Close();
(C#) .NET Windows Form Application เขียนโปรแกรมบน Windows Form Application ด้วย .NET Framework
|
|
|
|
|
Date :
2011-09-28 10:20:05 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อีกตัวอย่างการ Insert ผ่าน Parameters ครับ
Code (C#)
//*** Insert to Database ***'
SqlConnection objConn = new SqlConnection();
string strConnString = null;
string strSQL = null;
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
strSQL = "INSERT INTO files (Name,FilesName,FilesType) VALUES (@sName,@sFilesName,@sFilesType)";
objConn.ConnectionString = strConnString;
objConn.Open();
SqlCommand objCmd = new SqlCommand(strSQL, objConn);
objCmd.Parameters.Add("@sName", SqlDbType.Binary).Value = this.txtName.Text;
objCmd.Parameters.Add("@sFilesName", SqlDbType.VarChar).Value = imbByte;
objCmd.Parameters.Add("@sFilesType", SqlDbType.Int).Value = strMIME;
objCmd.ExecuteNonQuery();
objConn.Close();
objConn = null;
Go to : (C#) ASP.NET SQL Server BLOB Binary Data and Parameterized Query
|
|
|
|
|
Date :
2011-09-28 10:21:13 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
โหห ขอบพระคุณพี่ๆ มาก ครับ
จะลองทำเเล้วมาบอกผล น่ะคัรบ ขอบคุณจริงๆ คัรบ
|
|
|
|
|
Date :
2011-09-28 10:29:37 |
By :
Noomyontrakit |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
sb.Remove(0, sb.Length);
sb.Append("insert into ตารางที่จะบันทึก(ID,FirsName,LastName)values('"+@ID+"','"+@FirsName+"','"+@LastName+"')");
string saveBat = sb.ToString();
com = new SqlCommand();
com.CommandType = CommandType.Text;
com.CommandText = saveBat;
com.Connection = con;
com.ExecuteNonQuery();
|
|
|
|
|
Date :
2011-09-28 10:34:02 |
By :
Testprogram |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มัน Error ตามภาพอะครับ
หรือผมทำผิดตรงไหนเอ่ย...
Code (C#)
SqlConnection objConn = new SqlConnection();
string strConnString = null;
string strsql = null;
strConnString = "Server =NOOMYONT-682684;UID=;Password=;database=SchoolSQL;Max Pool Size=400;Connect Timeout=600;";
strsql = "INSERT INTO SchoolSQL(ID,Fname,Sname) VALUES(@txtID,@txtFname,@Sname)";
objConn.ConnectionString = strConnString;
objConn.Open();
SqlCommand objcmd = new SqlCommand(strsql, objConn);
objcmd.Parameters.Add("@ID", SqlDbType.Binary).Value = this.txtID.Text;
objcmd.Parameters.Add("@txtFname",SqlDbType.VarChar).Value = this.txtFname.Text;
objcmd.Parameters.Add("@txtSname", SqlDbType.VarChar).Value = this.txtLname.Text;
objcmd.ExecuteNonQuery();
objConn.Close();
objConn = null;
|
|
|
|
|
Date :
2011-09-28 11:59:13 |
By :
Noomyontrakit |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ประมาณน่าจะได้นะครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DataEntry
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
SqlConnection Conn;
SqlCommand com;
SqlDataReader dr;
StringBuilder sb = new StringBuilder();
private void Form2_Load(object sender, EventArgs e)
{
string strConn;
strConn = "Data Source=.\\SQLExpress;Initial Catalog=Northwind;Integrated Security=True";
Conn = new SqlConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("กรุณาป้อนรหัสลูกค้า !!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
textBox1.Focus();
return;
}
sb.Remove(0, sb.Length);
sb.Append("INSERT INTO SchoolSQL(ID,Fname,Sname)VALUES(@txtID,@txtFname,@Sname)");
sb.Append(" VALUES (@CustomerID,@OrderDate)");
string sqlSave = sb.ToString();
com.CommandType = CommandType.Text;
com.CommandText = sqlSave;
com.Connection = Conn;
com.Parameters.Clear();
com.Parameters.Add("@txtID", SqlDbType.NChar).Value = textBox1.Text.Trim();
com.Parameters.Add("@txtFname", SqlDbType.NChar).Value = textBox2.Text.Trim();
com.Parameters.Add("@Sname", SqlDbType.NChar).Value = textBox3.Text.Trim();
com.ExecuteNonQuery();
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "รายงานผล", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
|
|
|
|
|
Date :
2011-09-28 13:34:24 |
By :
Testprogram |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
รบกวนอีกทีน่ะ ค่ะ มัน Error ตรง SqlConnection conn; น่ะค่ะ แก้แล้วก็เหมือนเดิม ช่วยหน่อยน่ะค่ะ ขอบคุณมากค่ะ
Code (C#)
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:14:20 |
By :
นานา |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทำได้แล้วน่ะค่ะ ลงฐานข้อมูลแล้วค่ะ ขอบคุณมากๆน่ะค่ะ (โอกาศหน้าจะมารบกวนอีกน่ะค่ะ)
|
|
|
|
|
Date :
2013-11-26 12:30:03 |
By :
ด้าดา |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|