[英]Cannot get FoxPro SQL statement to execute properly from C#
我希望這里有人可以幫助解決我在C#
和FoxPro database
遇到的問題。
在下面,您將找到從FoxPro(SQL Server)數據庫中讀取的完整方法。
我遇到的問題是在語句SqlDataReader FP_Reader = FoxProSQLCmd.ExecuteReader();
當我單步執行代碼並達到這一行時,它總是跳到catch例程。 我假設它是SQL語句中未格式化或無效的內容。 Visual Studio除了此消息外沒有給我任何錯誤
“在System.Data.dll中發生了類型為“ System.InvalidOperationException'的第一次機會異常”。
此消息顯示在“調試輸出”窗口中。
同樣,在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語句更改為read:while(FP_Reader.Read()){DatabaseCommentField = FP_Reader [“ dfree”]。ToString(); 現在,我的程序正在完全執行我想要的操作! –
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.