C#两个类和两个表链接

时间:2016-05-09 18:18:17

标签: c# mysql database class

我正在做一个项目,而且我已经陷入了困境 所以我为这两个类得到了两个表:

 use strict;
 use CGI;
 if ($MAX_SIZE_UPLOAD) { $CGI::POST_MAX=1024 * $MAX_SIZE_UPLOAD; } # Ko
 # File sizes are limited to 25K, larger files will return an 'Internal Server Error'

 my $query = new CGI;

 my $login    = $query->param('login');
 my $password = $query->param('pass');
 my $action   = $query->param('ac');

 my ($dir);

 if ($query->param('BT_Exit')) { $action = ''; }


 if ($action eq 'admin') {
print $query->header;
if ($login && $password)  {
    &admin($query, $login, $password);
} else {
    print &PagePassword($NAME_TITLE{'common_admin'});
}
 } elsif ($action eq 'upload') {
print $query->header;
if ($dir = &check_password('guest', $login, $password)) {
    print &Upload($query, $dir);
} else {
    print &BadPassword($NAME_TITLE{'common_member'});
}
 } else {
print $query->redirect($FORM_URL);
 }


sub Upload  {
my($query, $upload_dir) = @_;
my($file_query, $file_name, $size, $buff, $time, $bytes_count);
$size = $bytes_count =0;
$_ = $file_query = $query->param('file');
s/\w://;
s/([^\/\\]+)$//;
$_ = $1;
s/\.\.+//g;
s/\s+//g;
$file_name = $_;

if (! $file_name) {
    $_ = $NAME_TITLE{'EU_BadFN'};
    s/Value_FileName/$file_name/ig;
    &Error($_, 1);
}

if (-e "$upload_dir/$file_name") {
    $_ = $NAME_TITLE{'EU_FExist'};
    s/Value_FileName/$file_name/ig;
    &Error($_, 1);
}

open(FILE,">$upload_dir/$file_name") || &Error("Error opening file      $file_name for writing, error $!", 1);
binmode FILE;
$time=time();
while ($bytes_count = read($file_query,$buff,2096)) {
    $size += $bytes_count;
    print FILE $buff;
}
close(FILE);

if ((stat "$upload_dir/$file_name")[7] <= 0) {
    unlink("$upload_dir/$file_name");
    $_ = $NAME_TITLE{'EU_Size'};
    s/Value_FileName/$file_name/ig;
    &Error($_, 1);
} else {
    $time = time -$time;
    $_ = $NAME_TITLE{'Upload_Succes_txt'};
    s/Value_FileName/$file_name/ig;
    s/Value_Size/$size/ig;
    s/Value_Time/$time/ig;
    &ResutPage($NAME_TITLE{'Upload_Succes'}, $_);
 }
 }

CREATE TABLE [dbo].[T_Artikli] (
[Id]       INT            IDENTITY (1, 1) NOT NULL,
[Naziv]    NVARCHAR (100) NOT NULL,
[Sifra]    VARCHAR (13)   NOT NULL,
[Vp]       FLOAT (53)     NOT NULL,
[MP]       FLOAT (53)     NOT NULL,
[Napomena] NVARCHAR (300) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

'Artikl'课程及其桌子'T_Artikli'就像一个魅力,问题在于另一个。这段代码应该将值存储在表中:

CREATE TABLE [dbo].[T_Stanje] (
[Id]       INT           IDENTITY (1, 1) NOT NULL,
[Trenutno] INT           NOT NULL,
[Naruceno] INT           NOT NULL,
[Datum]    DATE          NOT NULL,
[Firma]    NVARCHAR (40) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_T_Stanje_T_Artikli] FOREIGN KEY ([Id]) REFERENCES [dbo].[T_Artikli] ([Id])
);

它返回此错误:

  

System.Data.dll中出现未处理的“System.Data.SqlClient.SqlException”类型异常

     

附加信息:INSERT语句与FOREIGN KEY约束“FK_T_Stanje_T_Artikli”冲突。冲突发生在数据库“C:\ USERS \ MORSUS \ DOCUMENTS \ VISUAL STUDIO 2015 \ PROJECTS \ SEMINARSKI \ EVIDENCIJA \ EVIDENCIJA \ BIN \ DEBUG \ EVIDENCIJADB.MDF”,表“dbo.T_Artikli”,列'Id'。

在这一行:

private void btnSacuvaj_Click(object sender, EventArgs e)
{
    CStanje sta = new CStanje();
    sta.Trenutno = Int32.Parse(txtTrenutno.Text);
    sta.Naruceno = Int32.Parse(txtNaruceno.Text);
    sta.Firma = txtFirma.Text;
    sta.Datum = DateTime.Parse(dtDatum.Text);
    Artikl art = new Artikl();
    art.ID = Int32.Parse(cbArtikli.SelectedValue.ToString());
    sta.Artikl = art;
    Console.WriteLine(sta.Trenutno);
    Console.WriteLine(sta.Naruceno);
    Console.WriteLine(sta.Firma);
    Console.WriteLine(sta.Datum);
    Console.WriteLine(art.ID);
    sta.dodajStanje();
    indeksSelektovanog = dgStanje.Rows.Count;
}

我认为我在sql中做错了什么。有人能指出我吗?

关于sql的另一个问题,为什么我的sql表只保留了一段时间,例如3-4个程序运行。一段时间之后,数据被清除后不知道确切的时间......并且复制到输出目录设置为复制如果更新。

可以自由地要求更多信息。

1 个答案:

答案 0 :(得分:3)

是的,这个约束在下面是错误的;您将T_Stanji表的主键作为主键和T_Artikli表的外键,这是不可能发生的。请注意使用相同的列Id。

CONSTRAINT [FK_T_Stanje_T_Artikli] FOREIGN KEY ([Id]) REFERENCES [dbo].[T_Artikli] ([Id])

您需要将ArtikliID添加到T_Stanje表:

[ArtikliID] INT (NULL | NOT NULL)

然后将此约束更改为:

CONSTRAINT [FK_T_Stanje_T_Artikli] FOREIGN KEY ([ArtikliID]) REFERENCES [dbo].[T_Artikli] ([Id])

这样就可以映射这个新列的外键。