自动创建orcl表

时间:2023-03-10 06:22:19
自动创建orcl表

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Holworth.Utility;

namespace ConsoleApplication22
{
public enum OrclType
{
字段 = 0,
类型 = 1,
长度 = 2
};

class Program
{
/// <summary>
/// dic字典用于存储特征类型在Excel中的列索引,后续得到使用子段列对应的值是便可通过此字典进行抽象
/// </summary>
static Dictionary<string, int> dic = new Dictionary<string, int>();
static void Main(string[] args)
{
string path = @"d:\rskbook.xls";
string fileName = @"Sheet1$";
DataTable dt = ExcelUtil.GetExcelSheetContent(path, fileName);

DataRow dr = dt.Rows[0];
for (int i = 0; i < dt.Columns.Count; i++)
{
string columnName = dr[i].ToString();
if (columnName == OrclType.字段.ToString())
{
dic.Add(columnName, i);
}
if (columnName == OrclType.类型.ToString())
{
dic.Add(columnName, i);
}
if (columnName == OrclType.长度.ToString())
{
dic.Add(columnName, i);
}
}

StringBuilder sb = new StringBuilder();
sb.AppendFormat(string.Format("create table tab1\r\n("));

for (int i = 1; i < dt.Rows.Count; i++)
{
DataRow row = dt.Rows[i];
int FiledIndex = getColIndex(OrclType.字段);
int TypeIndex = getColIndex(OrclType.类型);
string LengthStr = "";
if (row[TypeIndex].ToString() != "date" && row[getColIndex(OrclType.长度)].ToString().Trim() != "")
LengthStr += "(" + row[getColIndex(OrclType.长度)] + ")";
if (i != dt.Rows.Count - 1)
sb.AppendFormat(string.Format("{0} {1},", row[FiledIndex], row[TypeIndex].ToString() + LengthStr));
else
{
sb.AppendFormat(string.Format("{0} {1}", row[FiledIndex], row[TypeIndex].ToString() + LengthStr));
}
sb.Append("\r\n");
}
sb.AppendFormat(@")");
Console.WriteLine(sb.ToString());
Console.ReadKey();
}

public static int getColIndex(OrclType col)
{
return dic[col.ToString()];
return 0;
}
}
}