1

每当我运行此脚本时,我都会收到错误“从字符串转换日期和/或时间时转换失败”

它指向“While (objReader.Read())”部分

namespace MedicalDataExporter
{
    public partial class frmSales : Form
    {
        public frmSales()
        {
            InitializeComponent();
        }

        private void dtpFrom_ValueChanged(object sender, EventArgs e)
        {

        }

        private void btnExtract_Click(object sender, EventArgs e)
        {
            SqlConnection objConn = new SqlConnection("Data Source=XXXX;Initial Catalog=YYYY;Persist Security Info=True;User ID=blah;Password=blah");

            // String conStr = Properties.Settings.Default.MedpracConnectionString;
            // System.Data.SqlClient.SqlConnection objConn = new System.Data.SqlClient.SqlConnection(conStr);
            objConn.Open();

            SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(8), InvDate,3) AS InvDate,InvoiceNo,EmployerCode,TaxAmount + SubTotal AS Amount,'' AS Payment FROM Invoice WHERE (InvDate >= CONVERT(datetime, '"+dtpFrom.Text +"', 3)) AND (InvDate <= CONVERT(datetime, '"+dtpTo.Text+"',3))", objConn);

            SqlDataReader objReader;
            objReader = objCmd.ExecuteReader();

            System.IO.FileStream fs = new System.IO.FileStream("C:\\CMSExportedData\\Sales-" + DateTime.Now.ToString("dd-MM-yy") + ".txt", System.IO.FileMode.Create);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);

            int count = 0;
            while (objReader.Read())
            {

                for (int i = 0; i < 5; i++)
                {
                    if (!objReader.IsDBNull(i))
                    {
                        string s;
                        s = objReader.GetDataTypeName(i);
                        //MessageBox.Show(s);
                        if (objReader.GetDataTypeName(i) == "char")
                        {
                            sw.Write(objReader.GetString(i));
                        }
                        else if (objReader.GetDataTypeName(i) == "money")
                        {
                            sw.Write(objReader.GetSqlMoney(i).ToString());
                        }
                        else if (objReader.GetDataTypeName(i) == "nvarchar")
                        {
                            sw.Write(objReader.GetString(i));
                        }
                    }
                    if (i < 4)
                    {
                        sw.Write("\t");
                    }
                }
                count = count + 1;

                sw.WriteLine();

            }
            sw.Flush();
            fs.Close();
            objReader.Close();
            objConn.Close();
            MessageBox.Show(count + " records exported successfully.");
            this.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
        }
    }
}
4

1 回答 1

2

将您的字符串转换为日期时间的这一行会导致异常。

   CONVERT(datetime,'"+dtpFrom.Text +"', 3)

改成这个

CONVERT(datetime,'"+dtpFrom.Text +"', 0)

有关更多信息,请参阅日期时间转换

在你的 sql server 上测试这两个查询:

SELECT CONVERT(datetime, '11/12/2012', 3) //this causes the exception
SELECT CONVERT(datetime, '11/12/2012', 0)

在您的代码上:

SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(8), InvDate,3) AS InvDate,InvoiceNo,EmployerCode,TaxAmount + SubTotal AS Amount,'' AS Payment FROM Invoice WHERE (InvDate >= CONVERT(datetime, '"+dtpFrom.Text +"', 0)) AND (InvDate <= CONVERT(datetime, '"+dtpTo.Text+"',0))", objConn);

最好的祝福

于 2012-07-20T06:56:43.293 回答