我给你一个C#类 你可以生成Oracle建表语句吗
时间: 2024-01-14 21:04:38 浏览: 48
当然可以,以下是一个简单的 C# 类示例,可以用于生成 Oracle 数据库建表语句:
```csharp
using System;
using System.Reflection;
using System.Text;
public class OracleTableBuilder
{
public string BuildTableCreationScript<T>(string tableName)
{
var sb = new StringBuilder();
sb.AppendFormat("CREATE TABLE {0} (", tableName);
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
string columnName = property.Name;
string columnType = GetOracleType(property.PropertyType);
sb.AppendFormat("{0} {1},", columnName, columnType);
}
sb.Remove(sb.Length - 1, 1); // 移除最后一个逗号
sb.Append(")");
return sb.ToString();
}
private string GetOracleType(Type type)
{
if (type == typeof(int))
{
return "NUMBER";
}
else if (type == typeof(decimal))
{
return "NUMBER(18,2)";
}
else if (type == typeof(DateTime))
{
return "DATE";
}
else if (type == typeof(string))
{
return "VARCHAR2(50)";
}
else
{
throw new NotImplementedException(string.Format("Oracle type mapping not implemented for type {0}.", type.Name));
}
}
}
```
该类的 `BuildTableCreationScript` 方法接受一个类型参数并返回一个字符串,其中包含用于创建该类型对应的 Oracle 表的 SQL 语句。你可以使用以下代码调用该方法:
```csharp
OracleTableBuilder builder = new OracleTableBuilder();
string sql = builder.BuildTableCreationScript<MyClass>("MyTable");
```
其中,`MyClass` 是你想要创建表的类型名称,`MyTable` 是你想要创建的表的名称。你可以根据需要调整 `GetOracleType` 方法中的类型映射。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)