Using System.OleDb
1
// BEGIN C# CODE2
using System;3
using System.Text;4
using System.Data;5
using System.Data.OleDb;6

7
public class Batter8


{9
// Declare private fields.10
private string firstName;11
private string lastName;12
private char bats;13
private int ab;14
private int runs;15
private int hits;16
private int doubles;17
private int triples;18
private int homers;19
private int rbis;20
private int walks;21
private int ks;22
private int sb;23
24
// Constructor without supplied arguments25
public Batter()26

{27
this.firstName="";28
this.lastName="";29
this.bats = ' ';30
this.ab = 0;31
this.runs = 0;32
this.hits = 0;33
this.doubles = 0;34
this.triples = 0;35
this.homers = 0;36
this.rbis = 0;37
this.walks = 0;38
this.ks = 0;39
this.sb = 0;40
}41

42
// Constructor with all arguments supplied43
public Batter(string firstName, string lastName, char bats, int ab, 44
int runs, int hits, int doubles, int triples, int homers, 45
int rbis, int walks, int ks, int sb)46

{47
48
this.firstName = firstName;49
this.lastName = lastName;50
this.bats = bats;51
this.runs = runs;52
this.ab = ab;53
this.hits = hits;54
this.doubles = doubles;55
this.triples = triples;56
this.homers = homers;57
this.rbis = rbis;58
this.walks = walks;59
this.ks = ks;60
this.sb = sb;61
}62
63
// Properties with Get and Set accessors for private access fields 64
public string FirstName65

{66
get67

{68
return firstName;69
}70
set71

{72
firstName = value;73
}74
}75
76
public string LastName77

{78
get79

{80
return lastName;81
}82
set83

{84
lastName = value;85
}86
}87

88
public char Bats89

{90
get91

{92
return bats;93
}94
set95

{96
bats = value;97
}98
}99
100
public int AB101

{102
get103

{104
return ab;105
}106
set107

{108
ab = value;109
}110
}111
112
public int Runs113

{114
get115

{116
return runs;117
}118
set119

{120
runs = value;121
}122
}123
124
public int Hits125

{126
get127

{128
return hits;129
}130
set131

{132
hits = value;133
}134
}135
136
public int Doubles137

{138
get139

{140
return doubles;141
}142
set143

{144
doubles = value;145
}146
}147
148
public int Triples149

{150
get151

{152
return triples;153
}154
set155

{156
triples = value;157
}158
}159
160
public int Homers161

{162
get163

{164
return homers;165
}166
set167

{168
homers = value;169
}170
}171
172
public int RBIs173

{174
get175

{176
return rbis;177
}178
set179

{180
rbis = value;181
}182
}183
184
public int Walks185

{186
get187

{188
return walks;189
}190
set191

{192
walks = value;193
}194
}195
196
public int Ks197

{198
get199

{200
return ks;201
}202
set203

{204
ks = value;205
}206
}207
208
public int SB209

{210
get211

{212
return sb;213
}214
set215

{216
sb = value;217
}218
}219
220
// Overrided ToString method from System.Object which formats the player221
// info in a string suitable for console output.222
public override string ToString()223

{224
StringBuilder strb = new StringBuilder(500);225
strb.Append("Batting Statistics");226
strb.Append("\n=========================");227
strb.Append("\nName: " + firstName + " " + lastName);228
strb.Append("\nBats: " + bats);229
strb.Append("\nAB: " + ab);230
strb.Append("\nRuns: " + runs);231
strb.Append("\nHits: " + hits);232
strb.Append("\nDoubles: " + doubles);233
strb.Append("\nTriples: " + triples);234
strb.Append("\nHomers: " + homers);235
strb.Append("\nRBIs: " + rbis);236
strb.Append("\nWalks: " + walks);237
strb.Append("\nKs: " + ks);238
strb.Append("\nSB: " + sb);239
return strb.ToString();240
}241
242
public static void Main()243

{244
// Instantiates b as a new Batter object245
Batter b = new Batter();246
247
// Stores connection string and sql select statement as strings248
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;";249
strConnection += " Data Source=c:\\mlb.mdb;";250
strConnection +=" user id=; password=;";251
string strCommand = "SELECT * FROM Batters";252
253
OleDbConnection conn = new OleDbConnection(strConnection);254
OleDbDataAdapter adapter = new OleDbDataAdapter();255
adapter.SelectCommand = new OleDbCommand(strCommand, conn);256
try257

{258
conn.Open();259
Console.WriteLine("The connection is open");260
DataSet ds = new DataSet();261
adapter.Fill(ds);262
263
// Ideally you would load several batter records from your264
// database and loop through an array of batter objects.265
// This program assumes only one record exists in your 266
// data set.267
foreach(DataTable dt in ds.Tables)268
foreach(DataRow dr in dt.Rows)269

{270
b.FirstName = Convert.ToString(dr["FirstName"]);271
b.LastName = Convert.ToString(dr["LastName"]);272
b.Bats = Convert.ToChar(dr["Bats"]);273
b.AB = Convert.ToInt32(dr["AB"]);274
b.Runs = Convert.ToInt32(dr["Runs"]);275
b.Hits = Convert.ToInt32(dr["Hits"]);276
b.Doubles = Convert.ToInt32(dr["Doubles"]);277
b.Triples = Convert.ToInt32(dr["Triples"]);278
b.Homers = Convert.ToInt32(dr["Homers"]);279
b.RBIs = Convert.ToInt32(dr["RBIs"]);280
b.Walks = Convert.ToInt32(dr["Walks"]);281
b.Ks = Convert.ToInt32(dr["Ks"]);282
b.SB = Convert.ToInt32(dr["SB"]);283
}284
Console.WriteLine("Data was retrieved");285
}286
catch(OleDbException e)287

{288
Console.WriteLine("Error: {0}", e.Errors[0].Message);289
}290
finally291

{292
string connState = conn.State.ToString();293
if ( connState == "Open")294

{295
conn.Close();296
Console.WriteLine("The connection has been closed\n");297
}298
else299
Console.WriteLine("The connection was never open\n");300
}301
302
Console.WriteLine(b.ToString());303
// Code to keep console window open after program execution.304
// This is valuable if you are building your code from an IDE305
// like VS.NET or SharpDevelop.306
Console.Write("\nPress ENTER to continue");307
Console.Read();308
}309
}310
// END C# CODE311

浙公网安备 33010602011771号