无法从C#中正确执行FoxPro SQL语句

时间:2014-11-11 16:54:40

标签: c# sql-server-2008

我希望有人可以帮助解决我与C#FoxPro database之间的问题。

您将在下面找到从FoxPro(SQL服务器)数据库中读取的完整方法 我遇到的问题出现在SqlDataReader FP_Reader = FoxProSQLCmd.ExecuteReader();声明中 当我单步执行代码并点击此行时,它总是跳转到catch例程。我假设它是SQL语句中没有格式化或无效的东西。除了此消息之外,Visual Studio不会给我任何错误

  

"类型" System.InvalidOperationException'的第一次机会异常   发生在System.Data.dll"。

此消息显示在“调试”输出窗口中。

另外,在SQL语句字符串FoxProSQLCommand = "SELECT dfree FROM dbo.gph_description_master WHERE dfree = 'CALIFORNIA BRANDS'";中   我无法使用SELECT *语句,因为我没有数据库中所有字段的访问权限。

下面的SQL语句从SQL Server管理工作室

正确运行并执行
SELECT TOP 100000 [dfree]
      FROM [INTRANET].[dbo].[gph_description_master]
      WHERE dfree = 'California brands'

,代码是:

private string GetFilenameFromFoxPro(string TemplateNum)
    {
        string TemplateFileNameInFoxPro = TemplateNum.Substring(8); 
        // string FoxProCommand = "SELECT dfree FROM dbo.gph_description_master WHERE tempno='" +                   TemplateFileNameInFoxPro + "'";  // use this command once access has been granted to column 'tempno' in database table.

        string FoxProSQLCommand = "SELECT dfree FROM dbo.gph_description_master WHERE dfree =     'CALIFORNIA BRANDS'";  // This is basically test code at the moment.

        //  string FoxProCommand = "SELECT [dfree] FROM [INTRANET].[dbo.gph_description_master]  WHERE dfree = 'CALIFORNIA BRANDS'";

        string DatabaseCommentField = "";
        SqlConnection FoxProDB = new SqlConnection();


        try 
           {
           FoxProDB.ConnectionString = ("User id=FoxProTemps;" +  
                                        "password=TemplatesRule!;" +          
                                        "Data Source=SQLPROD01;" +        // This is the server
                                        "Initial Catalog=INTRANET;"       // This is the database     
                                        ); 
           FoxProDB.Open();
           MessageBox.Show("FoxPro OPENED!");
           }

        catch
          {
            MessageBox.Show("FoxPro did not open!");
            return("Cannot Connect to TemplatesDatabase");
          }

        try
          {
            SqlCommand FoxProSQLCmd = new SqlCommand(FoxProCommand);              
            SqlDataReader FP_Reader = FoxProSQLCmd.ExecuteReader();  // This is the line that is giving me grief.

            while (FP_Reader.Read())
              {
                DatabaseCommentField = FP_Reader.ToString();
              }
          }
        catch
          {
            MessageBox.Show("SQL Command or SQL Reader did not work!");
            return (TemplateFileNameInFoxPro);
          }
            // ToDo:
            // Add code to parse Database comment fields so Template filename can be extracted
            // TemplateFileNameInFoxPro = DatabaseCommentField          
        FoxProDB.Close();
        return TemplateFileNameInFoxPro;

    }

请分享您的想法,建议或批评 我试图尽可能多地提供信息。

谢谢, MTH

1 个答案:

答案 0 :(得分:0)

终于搞定了!我将我的SQL语句更改为:" SELECT dfree FROM dbo.gph_description_master WHERE tempno ='" + TemplateFileNameInFoxPro +"'"然后将连接字符串添加到SQL命令:SqlCommand FoxProSQLCmd = new SqlCommand(FoxProCommand5,FoxProDB);然后将while语句更改为:while(FP_Reader.Read()){DatabaseCommentField = FP_Reader [" dfree"]。ToString();现在我的程序正在做我想要的! -