我有一个大的SQL语句,我想在c#中运行,但是,我看到的所有示例都是针对长度为一行的SQL语句。
到目前为止,这是我的C#代码。在我的代码之后,我已经包含了查询,我想要运行。
<小时/> C#查询代码
private void button4_Click(object sender, EventArgs e)
{
System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection();
conn.ConnectionString = "Driver={Oracle in OraClient11g_home1};" +
"Dbq=" + "database" +
";Uid=" + "admin" + ";Pwd=" + "admin" + ";";
try
{
conn.Open();
OdbcCommand com = new OdbcCommand("select count(*) from user_tables;", conn);
com.CommandType = CommandType.Text;
OdbcDataReader rdr = com.ExecuteReader();
while (rdr.Read())
{
var mystring = rdr.GetString(0);
MessageBox.Show(mystring);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}
<小时/> 查询运行
SELECT user_id,
user_name,
user_name_full,
(SELECT user_name
FROM MD_SITE_USER
WHERE user_id = u.GROUP_ID)
assigned_user_group,
user_or_group,
CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.GROUP_ID) = '+'
THEN
'Y'
ELSE
'N'
END
"Assigned By Group?",
CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.user_id) = '+'
THEN
'Y'
ELSE
'N'
END
"Assigned By User?"
FROM MD_SITE_USER u
WHERE user_name IS NOT NULL
AND ( CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a,
MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.GROUP_ID) = '+'
THEN
'Y'
ELSE
'N'
END = 'Y'
OR CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a,
MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.user_id) = '+'
THEN
'Y'
ELSE
'N'
END = 'Y')
ORDER BY user_or_group, user_name;
答案 0 :(得分:6)
这样做......或者使用StringBuilder类......
new OdbcCommand(@"SELECT user_id,
user_name,
user_name_full,
(SELECT user_name
FROM MD_SITE_USER
WHERE user_id = u.GROUP_ID)
assigned_user_group,
user_or_group,
CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.GROUP_ID) = '+'
THEN
'Y'
ELSE
'N'
END
'Assigned By Group?',
CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.user_id) = '+'
THEN
'Y'
ELSE
'N'
END
'Assigned By User?'
FROM MD_SITE_USER u
WHERE user_name IS NOT NULL
AND ( CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a,
MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.GROUP_ID) = '+'
THEN
'Y'
ELSE
'N'
END = 'Y'
OR CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a,
MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.user_id) = '+'
THEN
'Y'
ELSE
'N'
END = 'Y')
ORDER BY user_or_group, user_name;", conn);
答案 1 :(得分:4)
您可以使用string literal
符号{/ 1>}来@
string longstring = @"SELECT
*
FROM MyTable
";
请注意,在您的情况下,您的查询中包含"
。要逃避这些,请将其替换为""
。
string myQuery = @"SELECT user_id,
user_name,
user_name_full,
(SELECT user_name
FROM MD_SITE_USER
WHERE user_id = u.GROUP_ID)
assigned_user_group,
user_or_group,
CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.GROUP_ID) = '+'
THEN
'Y'
ELSE
'N'
END
""Assigned By Group?"",
CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.user_id) = '+'
THEN
'Y'
ELSE
'N'
END
""Assigned By User?""
FROM MD_SITE_USER u
WHERE user_name IS NOT NULL
AND ( CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a,
MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.GROUP_ID) = '+'
THEN
'Y'
ELSE
'N'
END = 'Y'
OR CASE
WHEN (SELECT o.grant_or_revoke
FROM MD_SITE_APP_ACTION a,
MD_SITE_APP_ACTION_OWNER o
WHERE a.action_id = o.action_id
AND a.action_name = 'Admin.start'
AND o.user_id = u.user_id) = '+'
THEN
'Y'
ELSE
'N'
END = 'Y')
ORDER BY user_or_group, user_name;
";