如何查找输入值SQL Server的主键


CREATE TABLE Thesis 
(
ThesisNo numeric(8,0) NOT NULL,
AuthorID int NOT NULL,
EnstituteID int NOT NULL,
SupervisorID int NOT NULL,
Title nvarchar(100)NOT NULL,
Abstract nvarchar(500)NOT NULL,
Pages int NOT NULL,
SumbitDate datetime NOT NULL,
[Type] nchar(30) NOT NULL,
[Language] nchar(20) NOT NULL,
PRIMARY KEY (ThesisNo),
FOREIGN KEY (EnstituteID) REFERENCES Enstitute(EnstituteID),
FOREIGN KEY (AuthorID) REFERENCES Author(AuthorID),
FOREIGN KEY (SupervisorID) REFERENCES Supervisor(SupervisorID)
)
CREATE TABLE Person
(
PersonID int NOT NULL,
PersonFullName nvarchar(70) NOT NULL,
PRIMARY KEY (PersonID)
)

我使用的是用C#编写的Windows窗体应用程序。

我把作者作为一个字符串,但在论文中添加外键时,我需要添加该作者的主键,这样我就可以访问联系人表并找到那个人。如何查找输入的人名的主键?

string query = "INSERT INTO Thesis (AuthorID,EnstituteID,SupervisorID,CoSupervisorID,Title,Abstract,Pages,SubmitDate,Type,Language)" +
"VALUES (@author,@enstitute,@supervisor,@cosupervisor,@title,@abstract,@pages,@submitdate,@type,@language) ";

AuthorID或其他ID成员是int和外键,但输入的值是字符串@author @enstitute, @supervisor, @cosupervisor

您可以在INSERT内部执行SELECTJOIN。例如,你可以说:

INSERT INTO Thesis  
(AuthorID,EnstituteID,SupervisorID,CoSupervisorID,Title,Abstract,Pages,SubmitDate,Type,Language)
SELECT
a.PersonID,
e.EnstituteID,
s.PersonID,
@cosupervisor,
@title,
@abstract,
@pages,
@submitdate,
@type,
@language
FROM Person a
JOIN Enstitute e ON e.Name = @enstitute
LEFT JOIN Person s ON s.Name = @supervisor
WHERE a.Name = @author;

在值可以为null的情况下,应该使用LEFT JOIN

请注意:如果名称不是唯一的,您将在插入中获得多行。


如果将两个表一起插入,则可以在第一个INSERT之后立即使用语句中的函数SCOPE_IDENTITY()来获得IDENTITY值。或者,也可以将OUTPUT子句用于表变量中。

如果将作者的姓名作为字符串,则可以使用简单的查询在Person表中进行搜索赞=>
1.Select PersonID FROM Person Where PersonFullName Like '%Author Name%'
2.Select PersonID FROM Person Where PersonFullName='Author Name'
但是使用这些查询存在一些问题。如果使用第一个,那么查询返回多行的可能性很小。如果你使用第二个,那么你就有机会没有得到任何一排。

最后,我的训诫并不是把作者当作一根绳子。使用一个组合框,将所有作者加载到该组合框中,然后选择特定作者并保存作者id。

该解决方案的新更新(使用组合框(:
如果您不知道如何将列表绑定到组合框以及如何使用SelectedIndexChanged事件,请检查给定的链接->
如何将列表绑定到组合框?
获取组合框的选定值

public class Person //Your Person Class
{
public int PersonID { get; set; }
public string PersonFullName { get; set; }
} 
public partial class Form1 : Form //Your form class where you use combo box
{
public Form1()
{
InitializeComponent();
loadCombo();
}
void loadCombo() //load dummy data to combo
{
//Load Combobox with List Of Persons
List<Person> persons = new List<Person>();
persons.Add(new Person() { PersonID = 1, PersonFullName = "Vikbaris" });
persons.Add(new Person() { PersonID = 2, PersonFullName = "Linker-SJNF" });

var bindingSource1 = new BindingSource();
bindingSource1.DataSource = persons;
comboBox1.DataSource = bindingSource1.DataSource;
comboBox1.DisplayMember = "PersonFullName";
comboBox1.ValueMember = "PersonID";
}
// Selected AuthorID event callback  
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cmb = (ComboBox)sender;
int selectedIndex = cmb.SelectedIndex;
Person selectedValue = (Person)cmb.SelectedValue;
int authorID = selectedValue.PersonID; //Here is your desire AuthorID
}
}

最新更新