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.SqlClient;
namespace Barcode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string strconn = @"Data Source=ASHWINI-LAPY\SQLEXPRESS;Initial Catalog=complete;Integrated Security=True;Pooling=False";
SqlDataReader reader = null;
SqlConnection conn = null;
conn = new SqlConnection(strconn);
conn.Open();
DateTime Dt_Time = DateTime.Now;
string Barcode = textBox1.Text;
SqlCommand cmd = new SqlCommand("select Barcode from table3 where @Barcode='" + textBox1.Text + "'", conn);
cmd.Parameters.AddWithValue("@Barcode", textBox1.Text);
reader = cmd.ExecuteReader();
if (reader != null && reader.HasRows)
{
//email exists in db do something
MessageBox.Show("Barcode Already Exists!!");
}
else
{
string strquery = string.Format("insert into table3 values('{0}','{1}')", Barcode, Dt_Time);
cmd = new SqlCommand(strquery, conn);
int count = (int)cmd.ExecuteNonQuery();
MessageBox.Show("Barcode:" + Barcode +
"\nTime" + Dt_Time);
}
我是新來的C#編碼,所以我試圖做到這像什麼,我在下面的代碼中提到,所以請人幫我。如果不存在,那麼插入別的節目信息「已經存在」
我想手動插入條形碼,當我按下按鈕時,必須檢查SQL Server數據庫是否存在該條形碼。如果沒有,它必須將該條形碼插入到數據庫中,但是如果它已經存在,則必須提供條形碼已存在的消息!
隨着插入條形碼我也插入系統日期和時間也在數據庫中。
首先這將是'...其中條碼= @Barcode」(很高興你正在使用的參數,同樣適用於刀片太) – V4Vendetta 2012-08-03 06:58:00
我只是更新整個代碼與C#.. – 2012-08-03 07:43:42
不要忘記標記爲接受,如果它適合你... – 2012-08-03 07:44:00