天天看點

c#生成資料庫實體類

一個生成資料庫實體類的代碼

在項目中可以很容易生成資料庫實體類,提高開發的效率

  1

c#生成資料庫實體類

using  System;

  2

c#生成資料庫實體類

using  System.Drawing;

  3

c#生成資料庫實體類

using  System.Collections;

  4

c#生成資料庫實體類

using  System.Collections.Specialized;

  5

c#生成資料庫實體類

using  System.ComponentModel;

  6

c#生成資料庫實體類

using  System.Windows.Forms;

  7

c#生成資料庫實體類

using  System.Data;

  8

c#生成資料庫實體類

using  System.IO;

  9

c#生成資料庫實體類

using  System.Text;

 10

c#生成資料庫實體類

using  System.Data.SqlClient;

 11

c#生成資料庫實體類

using  System.Data.SqlTypes;

 12

c#生成資料庫實體類

 13

c#生成資料庫實體類

namespace  WindowsApplication1

 14

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

 15

c#生成資料庫實體類
c#生成資料庫實體類

    /// <summary>

 16

c#生成資料庫實體類

    /// Form1 的摘要說明。

 17

c#生成資料庫實體類

    /// </summary>

 18

c#生成資料庫實體類

    public class Form1 : System.Windows.Forms.Form

 19

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

 20

c#生成資料庫實體類

        private System.Windows.Forms.ListBox listBox1;

 21

c#生成資料庫實體類

        private System.Windows.Forms.Button button1;

 22

c#生成資料庫實體類

        private System.Windows.Forms.ListView listView1;

 23

c#生成資料庫實體類

        private System.Windows.Forms.Button button2;

 24

c#生成資料庫實體類

        private System.Windows.Forms.ComboBox comboBox1;

 25

c#生成資料庫實體類

        private System.Windows.Forms.TextBox textBox1;

 26

c#生成資料庫實體類

        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;

 27

c#生成資料庫實體類

        private System.Windows.Forms.Label label1;

 28

c#生成資料庫實體類

        private System.Windows.Forms.ComboBox comboBox2;

 29

c#生成資料庫實體類

        private System.Windows.Forms.Label label2;

 30

c#生成資料庫實體類

        private System.Windows.Forms.Label label3;

 31

c#生成資料庫實體類

 32

c#生成資料庫實體類
c#生成資料庫實體類

        /// <summary>

 33

c#生成資料庫實體類

        /// 必需的設計器變量。

 34

c#生成資料庫實體類

        /// </summary>

 35

c#生成資料庫實體類

        private System.ComponentModel.Container components = null;

 36

c#生成資料庫實體類

 37

c#生成資料庫實體類

        public Form1()

 38

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

 39

c#生成資料庫實體類

            //

 40

c#生成資料庫實體類

            // Windows 窗體設計器支援所必需的

 41

c#生成資料庫實體類

            //

 42

c#生成資料庫實體類

            InitializeComponent();

 43

c#生成資料庫實體類

            this.MaximizeBox = false;

 44

c#生成資料庫實體類

            this.Text = "Create Tabel Class Tools";

 45

c#生成資料庫實體類

            this.button1.Text = "Import";

 46

c#生成資料庫實體類

            this.button2.Text = "Create";

 47

c#生成資料庫實體類

            listView1.View = View.Details;

 48

c#生成資料庫實體類

            listView1.GridLines = true;

 49

c#生成資料庫實體類

            listView1.Columns.Add("NO", -2, HorizontalAlignment.Left);

 50

c#生成資料庫實體類

            listView1.Columns.Add("ColumnName", -2, HorizontalAlignment.Left);

 51

c#生成資料庫實體類

            listView1.Columns.Add("Type", -2, HorizontalAlignment.Left);

 52

c#生成資料庫實體類

            listView1.Columns.Add("Length", -2, HorizontalAlignment.Left);

 53

c#生成資料庫實體類

            this.comboBox2.Items.Add("local");

 54

c#生成資料庫實體類

            this.comboBox2.Text = "local";

 55

c#生成資料庫實體類

            SQLDMO.NameList strServerNames;

 56

c#生成資料庫實體類

            SQLDMO.ApplicationClass sqldmoApp = new SQLDMO.ApplicationClass();

 57

c#生成資料庫實體類

            try

 58

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

 59

c#生成資料庫實體類

                strServerNames = sqldmoApp.ListAvailableSQLServers();

 60

c#生成資料庫實體類

                for(int i= 0 ;i<strServerNames.Count;i++)

 61

c#生成資料庫實體類

                    this.comboBox2.Items.Add(strServerNames.Item(i));

 62

c#生成資料庫實體類

            }

 63

c#生成資料庫實體類

            catch

 64

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

 65

c#生成資料庫實體類

                MessageBox.Show("Can not connect to other services");

 66

c#生成資料庫實體類

            }

 67

c#生成資料庫實體類

            finally

 68

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

 69

c#生成資料庫實體類

            sqldmoApp.Quit();

 70

c#生成資料庫實體類

            }

 71

c#生成資料庫實體類

 72

c#生成資料庫實體類

            ComBoxListDataLoad(this.comboBox2.Text);

 73

c#生成資料庫實體類

            this.textBox1.Text = string.Empty;

 74

c#生成資料庫實體類

            this.label1.Text = string.Empty;

 75

c#生成資料庫實體類

            this.label2.Text = "Servers:";

 76

c#生成資料庫實體類

            this.label3.Text = "DataBase:";

 77

c#生成資料庫實體類

            this.label1.AutoSize = true;

 78

c#生成資料庫實體類

            this.label2.AutoSize = true;

 79

c#生成資料庫實體類

            this.label3.AutoSize = true;

 80

c#生成資料庫實體類

 81

c#生成資料庫實體類

            //

 82

c#生成資料庫實體類

            // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼

 83

c#生成資料庫實體類

            //

 84

c#生成資料庫實體類

        }

 85

c#生成資料庫實體類

 86

c#生成資料庫實體類
c#生成資料庫實體類

        /// <summary>

 87

c#生成資料庫實體類

        /// 清理所有正在使用的資源。

 88

c#生成資料庫實體類

        /// </summary>

 89

c#生成資料庫實體類

        protected override void Dispose( bool disposing )

 90

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

 91

c#生成資料庫實體類

            if( disposing )

 92

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

 93

c#生成資料庫實體類

                if (components != null) 

 94

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

 95

c#生成資料庫實體類

                    components.Dispose();

 96

c#生成資料庫實體類

                }

 97

c#生成資料庫實體類

            }

 98

c#生成資料庫實體類

            base.Dispose( disposing );

 99

c#生成資料庫實體類

        }

100

c#生成資料庫實體類

101

c#生成資料庫實體類
c#生成資料庫實體類

        Windows 窗體設計器生成的代碼#region Windows 窗體設計器生成的代碼

102

c#生成資料庫實體類
c#生成資料庫實體類

        /// <summary>

103

c#生成資料庫實體類

        /// 設計器支援所需的方法 - 不要使用代碼編輯器修改

104

c#生成資料庫實體類

        /// 此方法的内容。

105

c#生成資料庫實體類

        /// </summary>

106

c#生成資料庫實體類

        private void InitializeComponent()

107

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

108

c#生成資料庫實體類

            this.listBox1 = new System.Windows.Forms.ListBox();

109

c#生成資料庫實體類

            this.button1 = new System.Windows.Forms.Button();

110

c#生成資料庫實體類

            this.listView1 = new System.Windows.Forms.ListView();

111

c#生成資料庫實體類

            this.button2 = new System.Windows.Forms.Button();

112

c#生成資料庫實體類

            this.comboBox1 = new System.Windows.Forms.ComboBox();

113

c#生成資料庫實體類

            this.textBox1 = new System.Windows.Forms.TextBox();

114

c#生成資料庫實體類

            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();

115

c#生成資料庫實體類

            this.label1 = new System.Windows.Forms.Label();

116

c#生成資料庫實體類

            this.comboBox2 = new System.Windows.Forms.ComboBox();

117

c#生成資料庫實體類

            this.label2 = new System.Windows.Forms.Label();

118

c#生成資料庫實體類

            this.label3 = new System.Windows.Forms.Label();

119

c#生成資料庫實體類

            this.SuspendLayout();

120

c#生成資料庫實體類

            // 

121

c#生成資料庫實體類

            // listBox1

122

c#生成資料庫實體類

            // 

123

c#生成資料庫實體類

            this.listBox1.ItemHeight = 12;

124

c#生成資料庫實體類

            this.listBox1.Location = new System.Drawing.Point(8, 40);

125

c#生成資料庫實體類

            this.listBox1.Name = "listBox1";

126

c#生成資料庫實體類

            this.listBox1.Size = new System.Drawing.Size(128, 208);

127

c#生成資料庫實體類

            this.listBox1.TabIndex = 0;

128

c#生成資料庫實體類

            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);

129

c#生成資料庫實體類

            // 

130

c#生成資料庫實體類

            // button1

131

c#生成資料庫實體類

            // 

132

c#生成資料庫實體類

            this.button1.Location = new System.Drawing.Point(424, 8);

133

c#生成資料庫實體類

            this.button1.Name = "button1";

134

c#生成資料庫實體類

            this.button1.Size = new System.Drawing.Size(64, 23);

135

c#生成資料庫實體類

            this.button1.TabIndex = 1;

136

c#生成資料庫實體類

            this.button1.Text = "button1";

137

c#生成資料庫實體類

            this.button1.Click += new System.EventHandler(this.button1_Click);

138

c#生成資料庫實體類

            // 

139

c#生成資料庫實體類

            // listView1

140

c#生成資料庫實體類

            // 

141

c#生成資料庫實體類

            this.listView1.Location = new System.Drawing.Point(144, 40);

142

c#生成資料庫實體類

            this.listView1.Name = "listView1";

143

c#生成資料庫實體類

            this.listView1.Size = new System.Drawing.Size(376, 240);

144

c#生成資料庫實體類

            this.listView1.TabIndex = 3;

145

c#生成資料庫實體類

            this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);

146

c#生成資料庫實體類

            // 

147

c#生成資料庫實體類

            // button2

148

c#生成資料庫實體類

            // 

149

c#生成資料庫實體類

            this.button2.Location = new System.Drawing.Point(8, 256);

150

c#生成資料庫實體類

            this.button2.Name = "button2";

151

c#生成資料庫實體類

            this.button2.Size = new System.Drawing.Size(56, 23);

152

c#生成資料庫實體類

            this.button2.TabIndex = 4;

153

c#生成資料庫實體類

            this.button2.Text = "button2";

154

c#生成資料庫實體類

            this.button2.Click += new System.EventHandler(this.button2_Click);

155

c#生成資料庫實體類

            // 

156

c#生成資料庫實體類

            // comboBox1

157

c#生成資料庫實體類

            // 

158

c#生成資料庫實體類

            this.comboBox1.Location = new System.Drawing.Point(288, 8);

159

c#生成資料庫實體類

            this.comboBox1.Name = "comboBox1";

160

c#生成資料庫實體類

            this.comboBox1.Size = new System.Drawing.Size(128, 20);

161

c#生成資料庫實體類

            this.comboBox1.TabIndex = 5;

162

c#生成資料庫實體類

            this.comboBox1.Text = "comboBox1";

163

c#生成資料庫實體類

            // 

164

c#生成資料庫實體類

            // textBox1

165

c#生成資料庫實體類

            // 

166

c#生成資料庫實體類

            this.textBox1.Location = new System.Drawing.Point(8, 312);

167

c#生成資料庫實體類

            this.textBox1.Multiline = true;

168

c#生成資料庫實體類

            this.textBox1.Name = "textBox1";

169

c#生成資料庫實體類

            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;

170

c#生成資料庫實體類

            this.textBox1.Size = new System.Drawing.Size(512, 136);

171

c#生成資料庫實體類

            this.textBox1.TabIndex = 6;

172

c#生成資料庫實體類

            this.textBox1.Text = "textBox1";

173

c#生成資料庫實體類

            // 

174

c#生成資料庫實體類

            // label1

175

c#生成資料庫實體類

            // 

176

c#生成資料庫實體類

            this.label1.Location = new System.Drawing.Point(8, 288);

177

c#生成資料庫實體類

            this.label1.Name = "label1";

178

c#生成資料庫實體類

            this.label1.Size = new System.Drawing.Size(512, 23);

179

c#生成資料庫實體類

            this.label1.TabIndex = 7;

180

c#生成資料庫實體類

            this.label1.Text = "label1";

181

c#生成資料庫實體類

            // 

182

c#生成資料庫實體類

            // comboBox2

183

c#生成資料庫實體類

            // 

184

c#生成資料庫實體類

            this.comboBox2.Location = new System.Drawing.Point(80, 8);

185

c#生成資料庫實體類

            this.comboBox2.Name = "comboBox2";

186

c#生成資料庫實體類

            this.comboBox2.Size = new System.Drawing.Size(121, 20);

187

c#生成資料庫實體類

            this.comboBox2.TabIndex = 8;

188

c#生成資料庫實體類

            this.comboBox2.Text = "comboBox2";

189

c#生成資料庫實體類

            // 

190

c#生成資料庫實體類

            // label2

191

c#生成資料庫實體類

            // 

192

c#生成資料庫實體類

            this.label2.Location = new System.Drawing.Point(8, 8);

193

c#生成資料庫實體類

            this.label2.Name = "label2";

194

c#生成資料庫實體類

            this.label2.Size = new System.Drawing.Size(48, 23);

195

c#生成資料庫實體類

            this.label2.TabIndex = 9;

196

c#生成資料庫實體類

            this.label2.Text = "label2";

197

c#生成資料庫實體類

            // 

198

c#生成資料庫實體類

            // label3

199

c#生成資料庫實體類

            // 

200

c#生成資料庫實體類

            this.label3.Location = new System.Drawing.Point(216, 8);

201

c#生成資料庫實體類

            this.label3.Name = "label3";

202

c#生成資料庫實體類

            this.label3.Size = new System.Drawing.Size(48, 23);

203

c#生成資料庫實體類

            this.label3.TabIndex = 10;

204

c#生成資料庫實體類

            this.label3.Text = "label3";

205

c#生成資料庫實體類

            // 

206

c#生成資料庫實體類

            // Form1

207

c#生成資料庫實體類

            // 

208

c#生成資料庫實體類

            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

209

c#生成資料庫實體類

            this.ClientSize = new System.Drawing.Size(552, 453);

210

c#生成資料庫實體類

            this.Controls.Add(this.label3);

211

c#生成資料庫實體類

            this.Controls.Add(this.label2);

212

c#生成資料庫實體類

            this.Controls.Add(this.comboBox2);

213

c#生成資料庫實體類

            this.Controls.Add(this.label1);

214

c#生成資料庫實體類

            this.Controls.Add(this.textBox1);

215

c#生成資料庫實體類

            this.Controls.Add(this.comboBox1);

216

c#生成資料庫實體類

            this.Controls.Add(this.button2);

217

c#生成資料庫實體類

            this.Controls.Add(this.listView1);

218

c#生成資料庫實體類

            this.Controls.Add(this.button1);

219

c#生成資料庫實體類

            this.Controls.Add(this.listBox1);

220

c#生成資料庫實體類

            this.Name = "Form1";

221

c#生成資料庫實體類

            this.Text = "Form1";

222

c#生成資料庫實體類

            this.Load += new System.EventHandler(this.Form1_Load);

223

c#生成資料庫實體類

            this.ResumeLayout(false);

224

c#生成資料庫實體類

225

c#生成資料庫實體類

        }

226

c#生成資料庫實體類

        #endregion

227

c#生成資料庫實體類

228

c#生成資料庫實體類
c#生成資料庫實體類

        /// <summary>

229

c#生成資料庫實體類

        /// 應用程式的主入口點。

230

c#生成資料庫實體類

        /// </summary>

231

c#生成資料庫實體類

        [STAThread]

232

c#生成資料庫實體類

        static void Main() 

233

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

234

c#生成資料庫實體類

            Application.Run(new Form1());

235

c#生成資料庫實體類

        }

236

c#生成資料庫實體類

237

c#生成資料庫實體類

        public void SqlConn()

238

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

239

c#生成資料庫實體類

            SqlConnection v_SqlConnection = new SqlConnection(ConnectString());

240

c#生成資料庫實體類

241

c#生成資料庫實體類

            try

242

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

243

c#生成資料庫實體類

                v_SqlConnection.Open();

244

c#生成資料庫實體類

                string strSql = "SELECT name AS tabelName FROM dbo.sysobjects WHERE (type = 'U') AND (name <> N'dtproperties') Order By tabelName"; 

245

c#生成資料庫實體類

                SqlCommand v_SqlCommand = new SqlCommand(strSql,v_SqlConnection);

246

c#生成資料庫實體類

                SqlDataReader v_SqlDataReader =v_SqlCommand.ExecuteReader();

247

c#生成資料庫實體類

                while(v_SqlDataReader.Read())

248

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

249

c#生成資料庫實體類

                    this.listBox1.Items.Add(v_SqlDataReader["tabelName"].ToString());

250

c#生成資料庫實體類

                }

251

c#生成資料庫實體類

            }

252

c#生成資料庫實體類

            catch(SqlException se)

253

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

254

c#生成資料庫實體類

                MessageBox.Show(se.ToString());

255

c#生成資料庫實體類

            }

256

c#生成資料庫實體類

            finally

257

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

258

c#生成資料庫實體類

                v_SqlConnection.Close(); 

259

c#生成資料庫實體類

            }

260

c#生成資料庫實體類

        }

261

c#生成資料庫實體類

262

c#生成資料庫實體類

        public string ConnectString()

263

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

264

c#生成資料庫實體類

            return  string.Format("Integrated Security=SSPI;Initial Catalog={0};Data Source=({1})",this.comboBox1.SelectedItem,this.comboBox2.Text);

265

c#生成資料庫實體類

        }

266

c#生成資料庫實體類

        private void Form1_Load(object sender, System.EventArgs e)

267

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

268

c#生成資料庫實體類

        }

269

c#生成資料庫實體類

270

c#生成資料庫實體類

        private void button1_Click(object sender, System.EventArgs e)

271

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

272

c#生成資料庫實體類

            this.listBox1.Items.Clear();  

273

c#生成資料庫實體類

            SqlConn();

274

c#生成資料庫實體類

        }

275

c#生成資料庫實體類

276

c#生成資料庫實體類

        private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)

277

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

278

c#生成資料庫實體類

            SqlConnection v_SqlConnection = new SqlConnection(ConnectString());

279

c#生成資料庫實體類

            try

280

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

281

c#生成資料庫實體類

                v_SqlConnection.Open();

282

c#生成資料庫實體類

                string strSql = string.Format("SELECT dbo.syscolumns.name AS ColumnName, dbo.systypes.name AS TypeName, dbo.syscolumns.length FROM dbo.syscolumns INNER JOIN dbo.sysobjects ON dbo.syscolumns.id = dbo.sysobjects.id INNER JOIN dbo.systypes ON dbo.syscolumns.xtype = dbo.systypes.xtype WHERE (dbo.sysobjects.name = '{0}') and (dbo.systypes.name <> N'sysname') Order By ColumnName",this.listBox1.SelectedItem); 

283

c#生成資料庫實體類

                SqlCommand v_SqlCommand = new SqlCommand(strSql,v_SqlConnection);

284

c#生成資料庫實體類

                SqlDataReader v_SqlDataReader =v_SqlCommand.ExecuteReader();

285

c#生成資料庫實體類

                int i= 0;

286

c#生成資料庫實體類

                this.listView1.Items.Clear();

287

c#生成資料庫實體類

                while(v_SqlDataReader.Read())

288

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

289

c#生成資料庫實體類

                    ListViewItem v_ListViewItem = new ListViewItem();

290

c#生成資料庫實體類

                    v_ListViewItem.SubItems[0].Text = i.ToString();

291

c#生成資料庫實體類

                    v_ListViewItem.SubItems.Add(v_SqlDataReader[0].ToString());

292

c#生成資料庫實體類

                    v_ListViewItem.SubItems.Add(v_SqlDataReader[1].ToString());

293

c#生成資料庫實體類

                    v_ListViewItem.SubItems.Add(v_SqlDataReader[2].ToString());

294

c#生成資料庫實體類

                    this.listView1.Items.Add(v_ListViewItem);

295

c#生成資料庫實體類

                    i++;

296

c#生成資料庫實體類

                }

297

c#生成資料庫實體類

            }

298

c#生成資料庫實體類

            catch(SqlException se)

299

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

300

c#生成資料庫實體類

                MessageBox.Show(se.ToString());

301

c#生成資料庫實體類

            }

302

c#生成資料庫實體類

            finally

303

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

304

c#生成資料庫實體類

                v_SqlConnection.Close(); 

305

c#生成資料庫實體類

            }

306

c#生成資料庫實體類

        }

307

c#生成資料庫實體類

308

c#生成資料庫實體類

        private void button2_Click(object sender, System.EventArgs e)

309

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

310

c#生成資料庫實體類

311

c#生成資料庫實體類

            if(this.listBox1.SelectedItem == null)

312

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

313

c#生成資料庫實體類

                MessageBox.Show("Please select one table,then try again","Opreat wrong");

314

c#生成資料庫實體類

                return ;

315

c#生成資料庫實體類

            }

316

c#生成資料庫實體類

            this.folderBrowserDialog1.ShowDialog();

317

c#生成資料庫實體類

            this.label1.Text = folderBrowserDialog1.SelectedPath +"\\" +  this.listBox1.SelectedItem.ToString() + ".cs";

318

c#生成資料庫實體類

319

c#生成資料庫實體類

            StreamWriter v_StreamWriter = new StreamWriter(folderBrowserDialog1.SelectedPath +"\\" +  this.listBox1.SelectedItem.ToString() + ".cs",false,Encoding.Unicode);

320

c#生成資料庫實體類

321

c#生成資料庫實體類

            string strText = "using System;\r\n using System.Data;\r\n namespace BE \r\n{ public class " + this.listBox1.SelectedItem + "\r\n {" ;       

322

c#生成資料庫實體類

323

c#生成資料庫實體類

            strText += "public " + this.listBox1.SelectedItem + "(){} \r\n";

324

c#生成資料庫實體類

            foreach(ListViewItem v_ListViewItem in this.listView1.Items)

325

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

326

c#生成資料庫實體類

                strText += "private " + StrSqlType(v_ListViewItem.SubItems[2].Text) + " _" + v_ListViewItem.SubItems[1].Text + ";\r\n";

327

c#生成資料庫實體類

            }

328

c#生成資料庫實體類

            strText += "\r\n\r\n";

329

c#生成資料庫實體類

330

c#生成資料庫實體類

            foreach(ListViewItem v_ListViewItem in this.listView1.Items)

331

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

332

c#生成資料庫實體類

                strText += "/// <summary> \r\n" + "///"+ v_ListViewItem.SubItems[1].Text  + " " +  v_ListViewItem.SubItems[2].Text + "\r\n/// </summary>\r\n";

333

c#生成資料庫實體類

                strText += "public " + StrSqlType(v_ListViewItem.SubItems[2].Text)  + " " + v_ListViewItem.SubItems[1].Text + "\r\n{ get{return _" + v_ListViewItem.SubItems[1].Text +";}\r\n" + "set{ _" + v_ListViewItem.SubItems[1].Text +  "=value; }}\r\n" ;

334

c#生成資料庫實體類

                strText += "\r\n";

335

c#生成資料庫實體類

            }

336

c#生成資料庫實體類

337

c#生成資料庫實體類

            strText += "}\r\n}";

338

c#生成資料庫實體類

            v_StreamWriter.Write(strText);

339

c#生成資料庫實體類

            v_StreamWriter.Close();

340

c#生成資料庫實體類

            this.textBox1.Text = strText;

341

c#生成資料庫實體類

        }

342

c#生成資料庫實體類

        public string StrSqlType(string strType)

343

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

344

c#生成資料庫實體類

            switch (strType)

345

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

346

c#生成資料庫實體類

                case "bigint":

347

c#生成資料庫實體類

                    return "long";

348

c#生成資料庫實體類

                case "int ":

349

c#生成資料庫實體類

                    return "int";

350

c#生成資料庫實體類

                case "smallint":

351

c#生成資料庫實體類

                    return "short";

352

c#生成資料庫實體類

                case "tinyint":

353

c#生成資料庫實體類

                    return "short";

354

c#生成資料庫實體類

                case "bit":

355

c#生成資料庫實體類

                    return "boolean";

356

c#生成資料庫實體類

                case "decimal":

357

c#生成資料庫實體類

                    return "decimal";

358

c#生成資料庫實體類

                case "numeric":

359

c#生成資料庫實體類

                    return "decimal";

360

c#生成資料庫實體類

                case "money":

361

c#生成資料庫實體類

                    return "string";

362

c#生成資料庫實體類

                case "smallmoney":

363

c#生成資料庫實體類

                    return "string";

364

c#生成資料庫實體類

                case "float":

365

c#生成資料庫實體類

                    return "float";

366

c#生成資料庫實體類

                case "real":

367

c#生成資料庫實體類

                    return "float";

368

c#生成資料庫實體類

                case "datetime":

369

c#生成資料庫實體類

                    return "DateTime";

370

c#生成資料庫實體類

                case "smalldatetime":

371

c#生成資料庫實體類

                    return "DateTime";

372

c#生成資料庫實體類

                case "char":

373

c#生成資料庫實體類

                    return "char";

374

c#生成資料庫實體類

                case "varchar":

375

c#生成資料庫實體類

                    return "string";

376

c#生成資料庫實體類

                case "text":

377

c#生成資料庫實體類

                    return "string";

378

c#生成資料庫實體類

                case "nchar":

379

c#生成資料庫實體類

                    return "string";

380

c#生成資料庫實體類

                case "nvarchar":

381

c#生成資料庫實體類

                    return "string";

382

c#生成資料庫實體類

                case "ntext":

383

c#生成資料庫實體類

                    return "string";

384

c#生成資料庫實體類

                case "binary":

385

c#生成資料庫實體類

                    return "byte";

386

c#生成資料庫實體類

                case "varbinary":

387

c#生成資料庫實體類

                    return "byte";

388

c#生成資料庫實體類

                case "image":

389

c#生成資料庫實體類

                    return "string";

390

c#生成資料庫實體類

                case "uniqueidentifier":

391

c#生成資料庫實體類

                    return "Guid";

392

c#生成資料庫實體類

                default :

393

c#生成資料庫實體類

                    return "string";

394

c#生成資料庫實體類

            }

395

c#生成資料庫實體類

        }

396

c#生成資料庫實體類

397

c#生成資料庫實體類

        public void ComBoxListDataLoad(string strServerName)

398

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

399

c#生成資料庫實體類

            SqlConnection v_SqlConnection = new SqlConnection(string.Format("Integrated Security=SSPI;Initial Catalog=Master;Data Source=({0})",strServerName));

400

c#生成資料庫實體類

            try

401

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

402

c#生成資料庫實體類

                v_SqlConnection.Open();

403

c#生成資料庫實體類

                string strSql = "SELECT TOP 100 PERCENT name AS DBName FROM dbo.sysdatabases ORDER BY name DESC"; 

404

c#生成資料庫實體類

                SqlCommand v_SqlCommand = new SqlCommand(strSql,v_SqlConnection);

405

c#生成資料庫實體類

                SqlDataReader v_SqlDataReader =v_SqlCommand.ExecuteReader();

406

c#生成資料庫實體類

                this.comboBox1.Text = "";

407

c#生成資料庫實體類

                while(v_SqlDataReader.Read())

408

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

409

c#生成資料庫實體類

                    this.comboBox1.Items.Add(v_SqlDataReader["DBName"]);

410

c#生成資料庫實體類

                }

411

c#生成資料庫實體類

            }

412

c#生成資料庫實體類

            catch(SqlException se)

413

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

414

c#生成資料庫實體類

                MessageBox.Show(se.ToString());

415

c#生成資料庫實體類

            }

416

c#生成資料庫實體類

            finally

417

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

418

c#生成資料庫實體類

                v_SqlConnection.Close(); 

419

c#生成資料庫實體類

            }

420

c#生成資料庫實體類

421

c#生成資料庫實體類

        }

422

c#生成資料庫實體類

423

c#生成資料庫實體類

        private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)

424

c#生成資料庫實體類
c#生成資料庫實體類
c#生成資料庫實體類

{

425

c#生成資料庫實體類

            this.listView1.Sort();

426

c#生成資料庫實體類

        }

427

c#生成資料庫實體類

    }

428

c#生成資料庫實體類

}

429

c#生成資料庫實體類

轉載于:https://www.cnblogs.com/Johson/archive/2007/08/28/database_class.html