我得到了添加外键约束的简单方法,只需制作外键 sql 命令的 txt 文件并给出 ';' 在每个 sql 命令之后并使用下面的代码它可以完美地工作......
private void FunAddForeignKeys()
{
SqlConnection clientConn = new SqlConnection(lconString);
if (clientConn.State == ConnectionState.Closed)
clientConn.Open();
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(GetSql("ForeignKeyQueries.txt"), clientConn);
try
{
Command.ExecuteNonQuery();
MessageBox.Show("Foreign keys added");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// Closing the connection should be done in a Finally block
clientConn.Close();
}
}
private string GetSql(string Name)
{
try
{
// Gets the current assembly.
Assembly Asm = Assembly.GetExecutingAssembly();
// Resources are named using a fully qualified name.
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);
// Reads the contents of the embedded file.
StreamReader reader = new StreamReader(strm);
return reader.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show("In GetSQL: " + ex.Message);
throw ex;
}
}