日期:2014-05-20  浏览次数:20846 次

深层的数据库类型错误?高手来
我运行getspdt方法的时候会报错:System.Data.SqlClient.SqlInternalConnectionTds 无法强制转换成System.Data.SqlClient.SqlInternalConnectionSmi

存储过程都参数都是varchar,在数据库里面执行没问题。

C# code

SqlConnection scn = new SqlConnection();//连接设定
    Access acc = new Access();   //操作基础类

    string beginDate = "";
    string enddate = "";
    string txtUserid = "";

    /// <summary>
    /// 国际惯例取字符串
    /// </summary>
    ///<param name="scn">连接的SqlDataConnection</param>
    private void getscnstr(SqlConnection Scn)
    {
        if (scn.ConnectionString == "")
        {
            Scn.ConnectionString = acc.ScnConnStr;
        }
    }

    /// <summary>
    /// 国际惯例芝麻开门
    /// </summary>
    /// <param name="scn">连接的SqlDataConnection</param>
    private void OpenConn(SqlConnection Scn)
    {
        if (scn.State == ConnectionState.Closed)
        {
            Scn.Open();
        }
    }

    private void MessageBoxShow(String Errstr)//信息提示
    {
        Response.Write("<scr" + "ipt> alert('" + Errstr + "') </scr" + "ipt>");
    }

 private DataTable GetSpdt(out DataTable spdt,string seUserid,string BeginDate,string Enddate)
    {
        try
        {
            spdt = null;
            string Userid = (string)(Session["LoginUser"]);
            string UserAuthority = (string)(Session["Authority"]);

            getscnstr(scn);
            OpenConn(scn);

            string sqlstr2 = "SELECT * FROM uvw_IsPrincipal WHERE FnAME='" + Userid + "' AND IsPrincipal=1 ";
            DataTable dt2 = acc.GetSereachData(scn, sqlstr2);
            if (dt2.Rows.Count == 0)
            {
                    seUserid = Userid;
            }

            getscnstr(scn);
            OpenConn(scn);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "usp_DalayReoprt";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = scn;

            cmd.Parameters.Add("@PositionCode", SqlDbType.VarChar);
            cmd.Parameters["@PositionCode"].Value = UserAuthority;
            cmd.Parameters.Add("@Code", SqlDbType.VarChar);
            cmd.Parameters["@Code"].Value = "'%"+seUserid+"%'";
            cmd.Parameters.Add("@BeingDate", SqlDbType.VarChar);
            cmd.Parameters["@BeingDate"].Value = "'"+BeginDate+"'";
            cmd.Parameters.Add("@EndDate", SqlDbType.VarChar);
            cmd.Parameters["@EndDate"].Value = "'" + Enddate + "'";

            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            //DataTable DDT = new DataTable();
            //DataSet ds=new DataSet();
            sda.Fill(spdt);
            //spdt = ds.Tables[0];
            return  spdt;
        }
        catch (Exception ex)
        {
            MessageBoxShow(ex.ToString());
            spdt = null;
            return spdt;
        }        
    }




------解决方案--------------------
C# code

 private void getscnstr(SqlConnection Scn)
    {
        if (scn.ConnectionString == "")
        {
            Scn.ConnectionString = acc.ScnConnStr;
        }
    }