Selasa, 31 Maret 2020
Minggu, 29 Maret 2020
INSERT INTO SQL SERVER combo,radio,numerik,waktu
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.Data.SqlClient;
using System.Windows.Forms;
namespace coba9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
String s = "data
source=ACER-PC\\SQLEXPRESS;database = hr;integrated security = SSPI";
SqlConnection c = new SqlConnection(s);
try
{
c.Open();
}
catch (SqlException exp)
{
string a = exp.Message;
}
string rad=string.Empty;
SqlTransaction sqltran = c.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO komponen(combo,radio,numerik,waktu)
VALUES('" + comboBox1.Text + "',@jk,'" +
numericUpDown1.Value
+
"',@date)");
SqlCommand com = new SqlCommand(sb.ToString(), c);
if (radioButton1.Checked)
com.Parameters.AddWithValue("@jk", "m");
else
com.Parameters.AddWithValue("@jk", "s");
com.Parameters.Add(new SqlParameter("@date",dateTimePicker1.Value.Date));
com.Transaction = sqltran;
com.ExecuteNonQuery();
sqltran.Commit();
}catch (SqlException ex){
sqltran.Rollback();
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
//string
sq = "select *from komponen where combo like'" + textBox1.Text +
"'";
string constr = "data
source=ACER-PC\\SQLEXPRESS;database = hr;integrated security = SSPI";
SqlConnection consql = new SqlConnection(constr);
try
{
consql.Open();
string cmd = "select*from
komponen where combo like '"+textBox1.Text+"'";
SqlDataAdapter dataadapter = new SqlDataAdapter(cmd, consql);
SqlCommandBuilder commandbuilder = new SqlCommandBuilder(dataadapter);
DataTable table = new DataTable();
dataadapter.Fill(table);
dataGridView1.DataSource =
table;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
consql.Close();
}
}
}
}Import data dari EXCEL ke DATAGRIDVIEW
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace coba7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel
WorkBook 97-2003|*.xls|Excel WorkBook|*.xlsx", ValidateNames = true })
{
if (ofd.ShowDialog() == DialogResult.OK){
String name = "Sheet1";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
ofd.FileName +
";Extended Properties='Excel 12.0
XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn =
new OleDbCommand("Select * From ["
+ name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
}
}
}
catch (Exception ex) { string errormsg = ex.ToString(); }
}
}
}
Langganan:
Postingan (Atom)