[英]How to add a if and else to SQL in c#
I'm still a beginner in c# and i'm using Visual Studio 2015. I have a problem because combobox only takes index. 我仍然是C#的初学者,而且我使用的是Visual Studio2015。我有一个问题,因为combobox只接受索引。 My combobox has a value of "admin, employee" so if the user select the 1 index (employee) it should be stored in the sql table of position as a "employee" but right now it stored only 0 and 1. * The position table has a datatype of nvarchar(20) * 我的组合框的值为“ admin,employee”,因此,如果用户选择1索引(员工),则应将其作为“员工”存储在sql位置表中,但现在它仅存储0和1。表的数据类型为nvarchar(20)*
private void aBtnSave_Click(object sender, EventArgs e)
{
string conString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Do not delete\insertUserExample.mdf;Integrated Security=True;Connect Timeout=30";
string query = "INSERT INTO userData (firstName,lastName,userName,password,contactNo,position) values('" + this.aTxtFirstName.Text + "', '" + this.aTxtLastName.Text + "', '" + this.aTxtUserName.Text + "', '" + this.aTxtPassword.Text + "', '" + this.aTxtContact.Text + "', '" + this.aUserPosition.SelectedIndex + "');";
SqlConnection sqlcon = new SqlConnection(conString);
SqlCommand sqlcom = new SqlCommand(query, sqlcon);
SqlDataReader sqlReader;
try {
sqlcon.Open();
sqlReader = sqlcom.ExecuteReader();
MessageBox.Show("User is Saved!");
while (sqlReader.Read()) {
}
aTxtFirstName.Clear();
aTxtLastName.Clear();
aTxtUserName.Clear();
aTxtPassword.Clear();
aTxtContact.Clear();
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
You can add a variable and condition to change combobox selected index to your string as below : 您可以添加变量和条件以将组合框选择的索引更改为字符串,如下所示:
var userTitleString = this.aUserPosition.SelectedIndex == 1 ? "employee" : "admin";
string query = "INSERT INTO userData (firstName,lastName,userName,password,contactNo,position) values('" + this.aTxtFirstName.Text + "', '" + this.aTxtLastName.Text + "', '" + this.aTxtUserName.Text + "', '" + this.aTxtPassword.Text + "', '" + this.aTxtContact.Text + "', '" + userTitleString + "');";
Or you can bind your data to comboBox when adding item to it: 或者,您可以在向其中添加项目时将数据绑定到comboBox:
ComboBoxItem comboItem = new ComboBoxItem();
item2.Text = "Admin";
item2.Value = "Admin";
ComboBoxItem comboItem2 = new ComboBoxItem();
item2.Text = "Employee";
item2.Value = "Employee";
List<ComboBoxItem> items = new List<ComboBoxItem> { comboItem, comboItem2 };
this.yourComboBox.DisplayMember = "Text";
this.yourComboBox.ValueMember = "Value";
this.yourComboBox.DataSource = items;
private void aBtnSave_Click(object sender, EventArgs e)
{
ComboboxItem selectedString = (ComboboxItem)yourComboBox.SelectedItem;
var userPosition= selectedString.Value;
string conString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Do not delete\insertUserExample.mdf;Integrated Security=True;Connect Timeout=30";
string query = "INSERT INTO userData (firstName,lastName,userName,password,contactNo,position) values('" + this.aTxtFirstName.Text + "', '" + this.aTxtLastName.Text + "', '" + this.aTxtUserName.Text + "', '" + this.aTxtPassword.Text + "', '" + this.aTxtContact.Text + "', '" + userPosition + "');";
SqlConnection sqlcon = new SqlConnection(conString);
SqlCommand sqlcom = new SqlCommand(query, sqlcon);
SqlDataReader sqlReader;
try {
sqlcon.Open();
sqlReader = sqlcom.ExecuteReader();
MessageBox.Show("User is Saved!");
while (sqlReader.Read()) {
}
aTxtFirstName.Clear();
aTxtLastName.Clear();
aTxtUserName.Clear();
aTxtPassword.Clear();
aTxtContact.Clear();
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
class ComboBoxItem
{
public string Text { get; set; }
public string Value { get; set; }
}
You can use code as below. 您可以使用以下代码。
aUserPosition.SelectedItem.Text , You will get selected text ie "Employee" or "Admin". aUserPosition.SelectedItem.Text ,您将获得选定的文本,即“ Employee”或“ Admin”。
string query = "INSERT INTO userData (firstName,lastName,userName,password,contactNo,position) values('" + this.aTxtFirstName.Text + "', '" + this.aTxtLastName.Text + "', '" + this.aTxtUserName.Text + "', '" + this.aTxtPassword.Text + "', '" + this.aTxtContact.Text + "', '" + this.aUserPosition.SelectedItem.Text + "');";
If I didn't get you wrong, you want to get the text in combobox not the Index. 如果我没有误会您的意思,您想在组合框而不是索引中获取文本。 Can you just try the code below ? 您可以尝试下面的代码吗?
aUserPosition.GetItemText(this.aUserPosition.SelectedItem)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.