我在用c#编写的WPF应用程序中遇到以下问题。我需要在sql server上创建一些临时表,从app中进行一些连接,然后从连接表中选择。在统计语言R中,我只创建两个SQL查询(一个包含#tables,另一个包含来自几个#tables的合并的最终select语句)并一个接一个地执行它们。仅在一个Query中执行查询仅在创建#temp表后返回NULL。
这是SQL语句#1:
DECLARE @from_time Date;
DECLARE @to_time Date;
CREATE TABLE #temp1
(
person_id float,
first_name varchar(100),
othercols...
)
INSERT INTO #temp1
SELECT DISTINCT
person_id, first_name, ...
FROM
campus.v_exam_registration_context context
FULL JOIN
campus.v_exam_timetable time
ON
CAST(context.examination_date AS DATETIME) = time.timetable_date
AND
context.examination_time_from = time.time_from
AND
context.examination_time_to = time.time_to
CREATE TABLE #temp2
(
exam_event_id float,
person_id float,
othercols...
)
INSERT INTO #temp2
SELECT DISTINCT
exam_event_id, person_id, excused, excused_reason, missed, modify_date, study_id, study_name,
person_exam_id, exam_in_course_id, subject, subject_unicode, personal_exam_no, exam_points, grade_description
FROM
campus.v_person_exam exam
WHERE
exam.person_id
IN
(
SELECT
#temp1.person_id
FROM
#temp1
WHERE
#temp1.attempt_counter > 1
AND
#temp1.timetable_date > @from_time
AND
#temp1.timetable_date < @to_time
)
CREATE TABLE #temp3
(
person_id float,
first_name varchar(100),
)
INSERT INTO #temp3
SELECT
#temp2.person_id, first_name, last_name, matriculation_number, attempt_counter, timetable_date, examination_date, semester, course_number, course_name,
exam_name, exam_type, component_name, course_area, module_number, module_name, credits,
FROM
#temp2
LEFT JOIN
#temp1
ON
#temp2.person_id = #temp1.person_id
AND
#temp2.exam_event_id = #temp1.exam_event_id
WHERE
#temp1.course_name IS NOT NULL
这是#2:
SELECT DISTINCT T1.*
FROM
#temp3 T1
INNER JOIN
(
SELECT *
FROM
#temp3
WHERE
#temp3.attempt_counter > 1
AND
#temp3.exam_points > 4
AND
#temp3.timetable_date > @from_time
AND
#temp3.timetable_date < @from_time
) as T2
ON
T1.person_id = T2.person_id
AND
T1.exam_name = T2.exam_name
ORDER BY T1.last_name ASC, T1.course_name DESC, T1.timetable_date ASC
但现在我需要将R脚本翻译成一个独立的exe文件。 这是我在c#中的代码块:
private void queryButton_Click(object sender, RoutedEventArgs e)
{
List<Student> temp = new List<Student>();
string pass = @pass_box.Password;
string dsn = "CampusNet";
String ConnectionString =
"DSN=" + dsn + ";" +
"UID=" + uid + ";" +
"PWD=" + pass;
OdbcConnection conn = new OdbcConnection(ConnectionString);
using (conn)
{
if (conn.State.ToString() == "Open")
{
conn.Close();
}
try
{
conn.Open();
var command = new OdbcCommand(sqlcommand, conn); \\sqlcommand is read from a textfile
command.Parameters.AddWithValue("@from_time", from_time);
command.Parameters.AddWithValue("@to_time", to_time);
command.CommandTimeout = 120;
var resultCommand = command.ExecuteNonQuery();
var query = new OdbcCommand(sqlquery, conn); \\sqlquery is read from a textfile
query.Parameters.AddWithValue("@from_time", from_time);
query.Parameters.AddWithValue("@to_time", to_time);
var resultQuery = query.ExecuteReader();
Results_Box.AppendText(resultQuery.HasRows.ToString());
while (resultQuery.Read())
{
temp.Add(new Student{
name = !result.IsDBNull(3) ? result.GetString(3) : null
});
Results_Box.AppendText("Test"); \\only for testing purposes, works if I select from existing table
}
resultQuery.Close();
results = temp;
Results_Box.Document.Blocks.Add(new Paragraph(
new Run("There are " + temp.Count().ToString() + " Hits")));
System.Windows.Forms.MessageBox.Show("Connected");
conn.Close();
}
catch (Exception E)
{
Results_Box.Document.Blocks.Add(new Paragraph(new Run("Connection failed")));
Results_Box.Document.Blocks.Add(new Paragraph(new Run(E.ToString())));
}
}
}
我是c#的新手,所以似乎第一个查询被执行但之后立即被删除而第二个select语句返回0次点击。如果我从非临时表中选择一切正常。由于第一个查询的持续时间大约等于相应的R-Query(25秒),我怀疑第一个查询是否正确执行,但是在第一个查询完成之前立即删除或第二个查询开始。创建一个组合查询在R和c#中都不起作用。我想坚持使用临时表,而不是使用##表,如果可能的话。
是否有一种特殊的方法可用于在c#中创建临时表? conn.open()
在using (conn){...}