SQL-Server to Access 处理 NULL
Posted
技术标签:
【中文标题】SQL-Server to Access 处理 NULL【英文标题】:SQL-Server to Access dealing with NULL 【发布时间】:2015-09-23 12:40:35 【问题描述】:我遇到了一个相当奇怪的问题。我正在编写将 MsSQL DB 转储到 Access 的函数(基于 this SO 帖子)。
在 MsSQL DB 中,我有一些 Float 字段。我已映射到 Access Double 的那些浮点字段。
MsSQL Float 字段有时包含 NULL,我无法将这些字段转移到访问。 Access 抱怨错误的值类型。如果我将该 NULL 设置为任何值,它就可以正常工作。
在我的代码中,我在 DataTable 中加载了一个表。像这样:
SqlDataAdapter adapter1 = new SqlDataAdapter("select * from tbl_values", conn);
DataSet dataSet = new DataSet();
adapter1.Fill(dataSet, "tbl_values");
我这样准备访问连接:
ADOX.Catalog catalog = new ADOX.Catalog();
catalog.Create(accessConnectionString);
//Create an Access connection and a command that we'll use
OleDbConnection accessConnection = new OleDbConnection(accessConnectionString);
OleDbCommand command = new OleDbCommand();
command.Connection = accessConnection;
//command.CommandType = CommandType.Text;
accessConnection.Open();
我这样迭代:
foreach (DataTable table in dataSet.Tables)
String columnsCommandText = "(";
foreach (DataColumn column in table.Columns)
String columnName = column.ColumnName;
String dataTypeName = column.DataType.Name;
String sqlDataTypeName = SetAccessFieldType(dataTypeName);
//String sqlDataTypeName = "text";//dataTypeName;
columnsCommandText += "[" + columnName + "] " + sqlDataTypeName + ",";
columnsCommandText = columnsCommandText.Remove(columnsCommandText.Length - 1);
columnsCommandText += ")";
command.CommandText = "CREATE TABLE " + table.TableName + columnsCommandText;
command.ExecuteNonQuery();
foreach (DataTable table in dataSet.Tables)
foreach (DataRow row in table.Rows)
int counter = 0;
String commandText = "INSERT INTO " + table.TableName + " VALUES (";
foreach (var item in row.ItemArray)
commandText += "'" + item.ToString() + "',";
commandText = commandText.Remove(commandText.Length - 1);
commandText += ")";
command.CommandText = commandText.ToString();
command.ExecuteNonQuery();
accessConnection.Close();
我使用这个辅助方法设置访问类型:
static public string SetAccessFieldType(string FieldName)
var AccessDataType = "";
switch (FieldName)
case "Int32":
AccessDataType = "long";
break;
case "String":
AccessDataType = "text";
break;
case "Double":
AccessDataType = "decimal";
break;
case "DateTime":
AccessDataType = "date";
break;
case "Boolean":
AccessDataType = "boolean";
break;
case "Byte[]":
AccessDataType = "date";//This is timeStamp datatype in Access
break;
default:
AccessDataType = "text";
break;
return AccessDataType;
我能做些什么来处理NULL的???
【问题讨论】:
【参考方案1】:代替这一行:
commandText += "'" + item.ToString() + "',";
试试这个:
if (item == null || item == DBNull.Value)
commandText += "NULL,";
else
commandText += "'" + item.ToString() + "',";
此外,请确保您在 Access 中的表具有配置为允许空值的相应列。见https://msdn.microsoft.com/en-us/library/bb177893(v=office.12).aspx。默认情况下,如果您在定义列时不添加NOT NULL
子句,则应该没问题。
【讨论】:
做到了:)。谢谢。以上是关于SQL-Server to Access 处理 NULL的主要内容,如果未能解决你的问题,请参考以下文章
#错误处理#Error 1304: Error writing to file: Verify that you have access to that directory while upgradi
node更新报错:checkPermissions Missing write access to /usr/lib/node_modules/n
[Hibernate]Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' n