如何使用C#将组合框值插入SQL Server

本文关键字:插入 SQL Server 组合 何使用 | 更新日期: 2023-09-27 18:20:49

我是C#的新手,所以请检查我的代码。它在这里的cmd.ExecuteNonQuery();中停止,但当我简单地插入日期时,它有效,但不使用组合框插入。

SQL查询是否正确?

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 newpro
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            object sel = comboBox1.SelectedValue;
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)'v11.0;AttachDbFilename=c:'users'abdul samad'documents'visual studio 2013'Projects'newpro'newpro'Database1.mdf;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO registor (Name, FullName, Password, Email, Gander) VALUES ('"+txtfname.Text+"','"+txtfname.Text+"', '"+txtuname.Text+"', '"+txtpass.Text+"', '"+txtemail.Text+"','"+comboBox1+"');",con);
            cmd.ExecuteNonQuery();
            cmd.Clone();
            MessageBox.Show("Record inserted");
            con.Close();
        }
    }
}

如何使用C#将组合框值插入SQL Server

您必须从组合框中获取选定的值。combobox1仅重新运行类名System.Windows.Forms.ComboBox

除其他外,建议使用参数。。像这样:

private void button1_Click(object sender, EventArgs e)
{
    using(SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)'v11.0;AttachDbFilename=c:'users'abdul samad'documents'visual studio 2013'Projects'newpro'newpro'Database1.mdf;Integrated Security=True"))
    {
        try
        {
            using (var cmd = new SqlCommand("INSERT INTO registor (Name, FullName, Password, Email, Gander) VALUES (@Name,@Fullname,@Password,@Email, @Gander)"))
            {
                cmd.Connection = con;   
                cmd.Parameters.Add("@Name", txtfname.Text);
                cmd.Parameters.Add("@Fullname", txtfname.Text);
                cmd.Parameters.Add("@Password", txtpass.Text);
                cmd.Parameters.Add("@Email", txtemail.Text);
                cmd.Parameters.Add("@Gander", comboBox1.GetItemText(comboBox1.SelectedItem));
                con.Open()
                if(cmd.ExecuteNonQuery() > 0) 
                {
                   MessageBox.Show("Record inserted"); 
                }
                else
                {
                   MessageBox.Show("Record failed");
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("Error during insert: " + e.Message);
        }
    }
}
public void insertfunction()
        { 
           string sqlconn = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
            SqlConnection cn = new SqlConnection(sqlconn);
            cn.Open();
            String query = "insert into PatientRecords values(@Patient_Name,@Cnic,@Phone,@Address,@Age,@Doctor_Reference,@City)";
            SqlCommand cmd = new SqlCommand(query,cn);
           // cmd.Parameters.AddWithValue("@Patient_Id", pid.Text);
            cmd.Parameters.AddWithValue("@Patient_Name", pname.Text);
            cmd.Parameters.AddWithValue("@Cnic", pcnic.Text);
            cmd.Parameters.AddWithValue("@Phone", pphone.Text);
            cmd.Parameters.AddWithValue("@Address", paddress.Text);
            cmd.Parameters.AddWithValue("@City", cmbopcity.GetItemText(cmbopcity.SelectedItem));
            cmd.Parameters.AddWithValue("@Age", page.Text);
            cmd.Parameters.AddWithValue("@Doctor_Reference", prefdoc.Text);

            if (cmd.ExecuteNonQuery() > 0)
            {
                MessageBox.Show("Record Successfully inserted");
            }
            else
            {
                MessageBox.Show("Record failed");
            }
            cn.Close();

        }