无法从C#中获取FoxPro SQL语句以正确执行
本文关键字:语句 执行 SQL FoxPro 获取 | 更新日期: 2023-09-27 18:30:05
我希望这里有人能帮我解决C#
和FoxPro database
的问题。
以下是从FoxPro(SQL server)数据库中读取的完整方法
我遇到的问题在语句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 management studio 中正确运行和执行
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
终于成功了!!我将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();}现在我的程序正在做我想要的事情!–