将两个输入分配给 SQL table 的单列
Assign two inputs to a single column of SQL table
我正在尝试使用小型表单收集用户数据并将这些数据插入到 Student table (db) 的列中。我能够插入除用户性别之外的所有信息,为此我使用了两个单选按钮(radioButton_male
和 radioButton_female
)。
这是我使用的 C# 代码:
仪表板形式:
private void Btn_Register_Click(object sender, EventArgs e)
{
Obj.StudentName = textBox_studentName.Text;
Obj.Age = int.Parse(textBox_age.Text);
Obj.Gender_m = radioButton_male.Text;
Obj.Gender_f = radioButton_female.Text;
Obj.Contact = int.Parse(textBox_contact.Text);
bool success = Obj.studentAdd(Obj);
if (success)
{
MessageBox.Show("Saved");
}
}
学生class
public string StudentName { get; set; }
public int Age { get; set; }
public string Gender_m { get; set; }
public string Gender_f { get; set; }
public int Contact { get; set; }
_
public bool studentAdd(Student obj)
{
SqlConnection conn = new SqlConnection(myconnstring);
bool success = false;
try
{
string sql = "INSERT INTO Students(FullName, Gender, ContactNo, Age) VALUES (@FullName, @Gender, @ContactNo, @Age)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@FullName", StudentName);
cmd.Parameters.AddWithValue("@Gender", Gender_m);
cmd.Parameters.AddWithValue("@Gender", Gender_f);
cmd.Parameters.AddWithValue("@ContactNo", Contact);
cmd.Parameters.AddWithValue("@Age", Age);
conn.Open();
int row = cmd.ExecuteNonQuery();
if (row > 0)
{
success = true;
}
else
{
success = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} finally
{
conn.Close();
}
return success;
}
我知道我不能简单地将两个条目分配给一个列,但我无法使用 If Else 或任何其他方式正确设置它。希望你能帮忙。谢谢。
您的设计应该只保留一个单个字段记录学生性别:
public string StudentName { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public int Contact { get; set; }
当您从 UI 收集表单数据时,您应该根据所选的单选按钮分配 'M'
或 'F'
。
您可能会考虑更改 table 设计以具有名字和姓氏,将性别设置为可以表示为字符串的位,并可能通过收集他们的出生日期来计算年龄。
人class
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool? Gender { get; set; }
public string GenderType => Gender != null && Gender.Value ? "Male" : "Female";
public DateTime? BirthDay { get; set; }
public string PhoneNumber { get; set; }
public override string ToString() => $"{FirstName} {LastName}";
}
查看数据(通常我们在应用程序中格式化)。请注意,在这种情况下计算年龄可能不准确,了解在运行时获取年龄而不是存储在数据库中 table 就足够了,因为随着时间的推移,年龄将不正确。此外,phone 数字采用特定格式,如果未给出该格式,则很容易失败。
SELECT Id,
FirstName,
LastName,
CASE
WHEN Gender = 1
THEN 'Male'
ELSE 'Female'
END AS Gender,
FORMAT(BirthDay, 'MM/dd/yyyy') AS BirthDay,
CONVERT(int,ROUND(DATEDIFF(hour,BirthDay,GETDATE())/8766.0,0)) AS Age,
SUBSTRING(phoneNumber, 1, 3) + '-' +
SUBSTRING(phoneNumber, 4, 3) + '-' +
SUBSTRING(phoneNumber, 7, 4) AS PhoneNumber
FROM dbo.People;
Table设计(列大小可以小于NVARCHAR(MAX))
CREATE TABLE dbo.People
(Id INT IDENTITY(1, 1) NOT NULL,
FirstName NVARCHAR(MAX) NULL,
LastName NVARCHAR(MAX) NULL,
Gender BIT NULL,
BirthDay DATETIME2(7) NULL,
PhoneNumber NVARCHAR(MAX) NULL,
CONSTRAINT PK_People PRIMARY KEY CLUSTERED(Id ASC)
)
我正在尝试使用小型表单收集用户数据并将这些数据插入到 Student table (db) 的列中。我能够插入除用户性别之外的所有信息,为此我使用了两个单选按钮(radioButton_male
和 radioButton_female
)。
这是我使用的 C# 代码: 仪表板形式:
private void Btn_Register_Click(object sender, EventArgs e)
{
Obj.StudentName = textBox_studentName.Text;
Obj.Age = int.Parse(textBox_age.Text);
Obj.Gender_m = radioButton_male.Text;
Obj.Gender_f = radioButton_female.Text;
Obj.Contact = int.Parse(textBox_contact.Text);
bool success = Obj.studentAdd(Obj);
if (success)
{
MessageBox.Show("Saved");
}
}
学生class
public string StudentName { get; set; }
public int Age { get; set; }
public string Gender_m { get; set; }
public string Gender_f { get; set; }
public int Contact { get; set; }
_
public bool studentAdd(Student obj)
{
SqlConnection conn = new SqlConnection(myconnstring);
bool success = false;
try
{
string sql = "INSERT INTO Students(FullName, Gender, ContactNo, Age) VALUES (@FullName, @Gender, @ContactNo, @Age)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@FullName", StudentName);
cmd.Parameters.AddWithValue("@Gender", Gender_m);
cmd.Parameters.AddWithValue("@Gender", Gender_f);
cmd.Parameters.AddWithValue("@ContactNo", Contact);
cmd.Parameters.AddWithValue("@Age", Age);
conn.Open();
int row = cmd.ExecuteNonQuery();
if (row > 0)
{
success = true;
}
else
{
success = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} finally
{
conn.Close();
}
return success;
}
我知道我不能简单地将两个条目分配给一个列,但我无法使用 If Else 或任何其他方式正确设置它。希望你能帮忙。谢谢。
您的设计应该只保留一个单个字段记录学生性别:
public string StudentName { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public int Contact { get; set; }
当您从 UI 收集表单数据时,您应该根据所选的单选按钮分配 'M'
或 'F'
。
您可能会考虑更改 table 设计以具有名字和姓氏,将性别设置为可以表示为字符串的位,并可能通过收集他们的出生日期来计算年龄。
人class
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool? Gender { get; set; }
public string GenderType => Gender != null && Gender.Value ? "Male" : "Female";
public DateTime? BirthDay { get; set; }
public string PhoneNumber { get; set; }
public override string ToString() => $"{FirstName} {LastName}";
}
查看数据(通常我们在应用程序中格式化)。请注意,在这种情况下计算年龄可能不准确,了解在运行时获取年龄而不是存储在数据库中 table 就足够了,因为随着时间的推移,年龄将不正确。此外,phone 数字采用特定格式,如果未给出该格式,则很容易失败。
SELECT Id,
FirstName,
LastName,
CASE
WHEN Gender = 1
THEN 'Male'
ELSE 'Female'
END AS Gender,
FORMAT(BirthDay, 'MM/dd/yyyy') AS BirthDay,
CONVERT(int,ROUND(DATEDIFF(hour,BirthDay,GETDATE())/8766.0,0)) AS Age,
SUBSTRING(phoneNumber, 1, 3) + '-' +
SUBSTRING(phoneNumber, 4, 3) + '-' +
SUBSTRING(phoneNumber, 7, 4) AS PhoneNumber
FROM dbo.People;
Table设计(列大小可以小于NVARCHAR(MAX))
CREATE TABLE dbo.People
(Id INT IDENTITY(1, 1) NOT NULL,
FirstName NVARCHAR(MAX) NULL,
LastName NVARCHAR(MAX) NULL,
Gender BIT NULL,
BirthDay DATETIME2(7) NULL,
PhoneNumber NVARCHAR(MAX) NULL,
CONSTRAINT PK_People PRIMARY KEY CLUSTERED(Id ASC)
)