本文介绍了如何将组合框中的数据插入到sql中?当我从combobox中将数据插入到sql中时,它在sql tabel中显示空白字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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;
using System.Data.SqlClient;
namespace studentMS
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection("Data Source=TAHIR-PC;Initial Catalog=StudentMS;Integrated Security=True");
SqlCommand cm;
private void Form2_Load(object sender, EventArgs e)
{
}
private void bnt_save_Click(object sender, EventArgs e)
{
//string s = "";
//if (cmb_SCourse.SelectedIndex >= 0)
// s = cmb_SCourse.Items[cmb_SCourse.SelectedIndex].ToString();
try
{
string sql="Insert into Students(Name,FatherName,address,PhoneNo,Email,Course,Batch,Teacher)values('"+txt_SName.Text+"','"+txt_SFname.Text+"','"+txt_SAddress.Text+"','"+txt_SPhoneNo.Text+"','"+txt_SEmail.Text+"','"+cmb_SCourse.SelectedItem+"','"+cmb_SCourse.SelectedItem+"','"+cmb_Steacher.SelectedItem+"')";
conn.Open();
cm = new SqlCommand(sql, conn);
cm.ExecuteNonQuery();
conn.Close();
label1.Visible = true;
label1.Text = "saved Successfully";
txt_SAddress.Text = txt_SEmail.Text = txt_SFname.Text = txt_SName.Text = txt_SPhoneNo.Text = "";
}
catch(Exception ex)
{
label1.Text = ex.Message;
}
}
}
}
推荐答案
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;
using System.Data.SqlClient;
namespace studentMS
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection("Data Source=TAHIR-PC;Initial Catalog=StudentMS;Integrated Security=True");
SqlCommand cm;
private void Form2_Load(object sender, EventArgs e)
{
}
private void bnt_save_Click(object sender, EventArgs e)
{
//string s = "";
//if (cmb_SCourse.SelectedIndex >= 0)
// s = cmb_SCourse.Items[cmb_SCourse.SelectedIndex].ToString();
try
{
string sql="Insert into Students(Name,FatherName,address,PhoneNo,Email,Course,Batch,Teacher)values('"+txt_SName.Text+"','"+txt_SFname.Text+"','"+txt_SAddress.Text+"','"+txt_SPhoneNo.Text+"','"+txt_SEmail.Text+"','"+cmb_SCourse.SelectedItem.text+"','"+cmb_SCourse.SelectedItem.text+"','"+cmb_Steacher.SelectedItem.text+"')";
conn.Open();
cm = new SqlCommand(sql, conn);
cm.ExecuteNonQuery();
conn.Close();
label1.Visible = true;
label1.Text = "saved Successfully";
txt_SAddress.Text = txt_SEmail.Text = txt_SFname.Text = txt_SName.Text = txt_SPhoneNo.Text = "";
}
catch(Exception ex)
{
label1.Text = ex.Message;
}
}
}
}
cmb_SCourse.SelectedItem
as
as
cmb_SCourse.Text
这篇关于如何将组合框中的数据插入到sql中?当我从combobox中将数据插入到sql中时,它在sql tabel中显示空白字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!