현재 SQL Server에서 C #을 통해 Access로 테이블을 다시 작성하고 있습니다.
이를 위해 SQL Server에서 사용되는 데이터 형식을 가져 와서 OleDbType 개체에 매핑합니다.
불행히도, Access에 대한 문을 실행하려고 할 때마다 "Create Table"문에 구문 오류가 있다는 예외가 발생합니다. 매핑 된 데이터 유형을 OleDbParameters가 아닌 텍스트로 추가하기 때문이라고 생각합니다.
OleDbParameter- "Create Table"문에 대한 열 이름과 데이터 유형을 포함하는 개체를 만드는 방법이 있습니까?
String accessConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Data
Source=" + filepath;
using (OleDbConnection accessConnection = new OleDbConnection(accessConnectionString))
{
ADOX.Catalog cat = new ADOX.Catalog();
cat.Create(accessConnectionString);
OleDbCommand oleCommand = new OleDbCommand();
oleCommand.Connection = accessConnection;
oleCommand.CommandType = CommandType.Text;
accessConnection.Open();
String columnsCommandText = "(";
for (int i = 0; i < reader.GetSchemaTable().Rows.Count; i++) // reader contains data from SQL Server
{
var column = reader.GetName(i); // name of attribute
SqlDbType type = (SqlDbType)(int)reader.GetSchemaTable().Rows[i]["ProviderType"]; // data type
var accessType = SQLAccessMapper.MapDataTypes(type);
columnsCommandText += " " + column + " " + accessType + ",";
}
columnsCommandText = columnsCommandText.Remove(columnsCommandText.Length - 1);
columnsCommandText += ")";
oleCommand.CommandText = "CREATE TABLE " + tablename + columnsCommandText;
oleCommand.ExecuteNonQuery(); // Exception
매퍼 :
static class SQLAccessMapper
{
public static OleDbType MapDataTypes(SqlDbType sqlDataType)
{
switch (sqlDataType)
{
case SqlDbType.Int:
return OleDbType.Integer;
case SqlDbType.SmallInt:
return OleDbType.SmallInt;
case SqlDbType.TinyInt:
return OleDbType.TinyInt;
case SqlDbType.Decimal:
return OleDbType.Decimal;
case SqlDbType.Float:
case SqlDbType.Real:
return OleDbType.Single;
case SqlDbType.BigInt:
return OleDbType.BigInt;
case SqlDbType.Char:
return OleDbType.Char;
case SqlDbType.NChar:
return OleDbType.WChar;
case SqlDbType.NText:
case SqlDbType.NVarChar:
case SqlDbType.Text:
return OleDbType.VarWChar;
case SqlDbType.VarChar:
return OleDbType.VarChar;
case SqlDbType.Time:
return OleDbType.DBTime;
case SqlDbType.Date:
return OleDbType.DBDate;
case SqlDbType.DateTime:
case SqlDbType.DateTime2:
case SqlDbType.DateTimeOffset:
case SqlDbType.SmallDateTime:
case SqlDbType.Timestamp:
return OleDbType.DBTimeStamp;
case SqlDbType.Binary:
return OleDbType.Binary;
case SqlDbType.VarBinary:
return OleDbType.VarBinary;
case SqlDbType.Money:
case SqlDbType.SmallMoney:
return OleDbType.Currency;
case SqlDbType.Bit:
return OleDbType.Boolean;
default: return OleDbType.Error;
}
}
}
예문 Create Table
:
CREATE TABLE GrTable(
GrtId Integer,
CaseId Integer,
GrDrg VarChar,
GrDrgText VarWChar,
Mdc VarChar,
MdcText VarWChar,
GrPartition VarChar,
Baserate Decimal,
LosUsed Integer,
Htp Integer,
PricePerDay Decimal,
Ltp Integer,
LtpPricePerDay Decimal,
AverLos Decimal,
AverlosPricePerDay Decimal,
Eff Decimal,
Std Decimal,
Adj Decimal,
Gst VarChar,
Pccl Integer,
PriceEff Decimal,
PriceStd Decimal,
PriceAdj Decimal,
DaysExcHtp Integer,
DaysBelowLtp Integer,
DaysBelowAverLos Integer,
TotalPrice Decimal,
BaseratePeriod VarChar)
주요 문제는 MapDataTypes에 있습니다. MS-Access 엔진에서 예상하는 문자열을 반환해야하며 지금처럼 일반 열거 형 SqlDbType을 문자열로 자동 변환하지 않아야합니다.
예를 들면. VarWChar 유형의 열의 경우 문자열 "TEXT"다음에 필드 크기를 전달해야하며 정수 유형의 필드에는 "INT"문자열이 필요합니다.
public static string MapDataTypes(SqlDbType sqlDataType)
{
switch (sqlDataType)
{
case SqlDbType.Int:
return "INT";
case SqlDbType.VarChar:
return "TEXT(80)";
... AND SO ON FOR EVERY POSSIBLE TYPE
}
}
물론 이것은 TEXT 필드의 크기 문제를 야기하지만 ColumnSize라는 열에서 유형을 찾는 데 사용 된 것과 동일한 GetSchemaTable에서이를 검색하고이 크기를 MapDataTypes 메서드에 전달할 수 있습니다.
public static string MapDataTypes(SqlDbType sqlDataType, int size)
{
switch (sqlDataType)
{
case SqlDbType.Int:
return "INT";
case SqlDbType.VarChar:
return "TEXT(" + size + )";
.....
}
}
이 SQL 데이터 유형 에서 허용되는 일부 유형을 찾을 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다