1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Data;5
using System.Data.SqlClient;6

7
namespace EOffice.Model8


{9
public class SendDoc10

{11
private string _strId;12
private string _strTitle;13
private string _strTopic;14
private string _strCode;15
private string _strMainSend;16
private string _strCopyReport;17
private string _strCopySend;18
private string _strIdea;19

20
private string _nDrafterId;21

22
private string _strLastID;23

24
private SqlConnection conn;25

26

27
public SendDoc()28

{29
}30

31
public SendDoc(string drafterid,string title,string topic,32
string code,string mainsend,string copyreport,33
string copysend,string idea)34

{35
this._nDrafterId = drafterid;36
this._strTitle = title;37
this._strTopic = topic;38
this._strCode = code;39
this._strMainSend = mainsend;40
this._strCopyReport = copyreport;41
this._strCopySend = copysend;42
this._strId = idea;43
}44

45
public string DrafterID46

{47

get
{ return _nDrafterId; }48

set
{ _nDrafterId = value; }49
}50
public string Title51

{52

get
{ return _strTitle; }53

set
{ _strTitle = value; }54
}55
public string Topic56

{57

get
{ return _strTopic; }58

set
{ _strTopic = value; }59
}60
public string Code61

{62

get
{ return _strCode; }63

set
{ _strCode = value; }64
}65
public string MainSend66

{67

get
{ return _strMainSend; }68

set
{ _strMainSend = value; }69
}70
public string CopyReport71

{72

get
{ return _strCopyReport; }73

set
{ _strCopyReport = value; }74
}75
public string CopySend76

{77

get
{ return _strCopySend; }78

set
{ _strCopySend = value; }79
}80
public string Idea81

{82

get
{ return _strIdea; }83

set
{ _strIdea = value; }84
}85

86
public SqlConnection Conn87

{88

set
{ conn = value; }89
}90

91
public string LastID92

{93

get
{ return _strLastID; }94
}95

96
public void Insert(SendDoc doc)97

{98
String strSQL = "Insert Into SendDoc (Title,Topic,Code,MainSend,CopyReport,CopySend,Idea,DrafterID)";99
strSQL += " values ('" + doc.Title + "'";100
strSQL += ",'" + doc.Topic + "'";101
strSQL += ",'" + doc.Code + "'";102
strSQL += ",'" + doc.MainSend + "'";103
strSQL += ",'" + doc.CopyReport + "'";104
strSQL += ",'" + doc.CopySend + "'";105
strSQL += ",'" + doc.Idea + "',";106
strSQL += doc.DrafterID;107
strSQL += ")";108
strSQL += " Select @@IDENTITY";109

110
SqlCommand cmd = new SqlCommand(strSQL, conn);111
112
conn.Open();113

114
_strLastID = cmd.ExecuteScalar().ToString();115
//cmd.ExecuteNonQuery();116

117
conn.Close();118
}119
}120
}121

代码参考96行的Insert函数。关键是SQL语句采用Insert ..... Select @@IDENTITY。
然后用SqlCommand的ExecuteScalar()方法,返回的值就是新插入记录的主键ID。
浙公网安备 33010602011771号