如题,
我想把1.1.1当string,不要自动转date类型,怎么办?

解决方案 »

  1.   

    不会吧,有这种问题??
    csv文件做数据源时,并没有指定某列是何类型的. 怎么会把1.1.1这种形式认成DATE?不懂了,关注.
      

  2.   

    odbc会自动加数据类型的我的代码如下:using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Data.Odbc;
    using System.Data.OleDb;
    using System.Security.Cryptography;namespace TestCSV
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button btnImport;
    private System.Windows.Forms.DataGrid dataGrid1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.btnImport = new System.Windows.Forms.Button();
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // btnImport
    // 
    this.btnImport.Location = new System.Drawing.Point(8, 16);
    this.btnImport.Name = "btnImport";
    this.btnImport.Size = new System.Drawing.Size(96, 32);
    this.btnImport.TabIndex = 2;
    this.btnImport.Text = "载入电子文档";
    this.btnImport.Click += new System.EventHandler(this.btnImport_Click);
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(0, 96);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(736, 368);
    this.dataGrid1.TabIndex = 3;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(880, 477);
    this.Controls.Add(this.dataGrid1);
    this.Controls.Add(this.btnImport);
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void btnImport_Click(object sender, System.EventArgs e)
    {
    try
    {
    string m_file=@"c:\\aaa.csv";
    DataSet ds=new DataSet();
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "CSV文档 (*.csv)|*.csv|所有文件 (*.*)|*.*";
    ofd.FilterIndex = 1;
    ofd.Title = "请选择您要打开的CSV文档";
    if(ofd.ShowDialog() == DialogResult.OK)
    {

    System.IO.File.Copy(ofd.FileName,m_file,true);

    OdbcConnection odbcConn= new OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=");
    OdbcCommand odbcCmd=odbcConn.CreateCommand();
    odbcCmd.CommandText="Select * from "+m_file; odbcConn.Open();

    OdbcDataReader odbcReader=odbcCmd.ExecuteReader();
    DataTable dt=new DataTable();
    for(int i=0;i<odbcReader.FieldCount;i++)
    {
    string colName=odbcReader.GetName(i);
    DataColumn col=new DataColumn(colName,System.Type.GetType("System.String"));
    dt.Columns.Add(col);
    } while(odbcReader.Read())
    {
    DataRow row=dt.NewRow(); for(int i=0;i<odbcReader.FieldCount;i++)
    {
    if(!odbcReader.IsDBNull(i))
    {
    object obj=odbcReader.GetFieldType(i);
    row[i]=obj;
    }
    } dt.Rows.Add(row);
    } odbcReader.Close();
    odbcConn.Close(); this.dataGrid1.DataSource=dt;
    if(System.IO.File.Exists(m_file))
    System.IO.File.Delete(m_file); MessageBox.Show("CSV文档打开成功!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
    this.dataGrid1.Enabled=true;
    }
    }
    catch (Exception ee)
    {
    MessageBox.Show(ee.ToString());
    }
    } }}
    csv文件内容如下:代码,名称
    1.1.1,合同表名称1
    2.1.1,合同表名称2
    3.1.1,合同表名称3
    4.1.1,合同表名称4
    5.1.1,合同表名称5
    6.1.1,sdf
      

  3.   

    try to set IMEX=1see
    http://support.microsoft.com/default.aspx?scid=kb;zh-CN;257819
    also see
    http://www.connectionstrings.com