使用c#查找给定表是否存在于数据库中的代码 [英] code for finding whether the given table is present in Database or not using c#

查看:58
本文介绍了使用c#查找给定表是否存在于数据库中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有问题,我有两个网页(Firstpage.aspx,second.aspx).在Firstpage.aspx中,我需要通过文本框传递表名来检查表是否存在于数据库中.如果表名存在,则应发出警报msg,如果不存在该表应转到second.aspx.使用< list string =">编写了代码我可以显示该表是否存在,但是当我试图重定向到另一页时,我为现有表编写的代码不起作用...
这是我的代码...`

在此处输入代码

Iam having a problem in my project I have two web pages(Firstpage.aspx,second.aspx). In Firstpage.aspx i need to check whether the table exist in the database by passing the table name through textbox .If the table name exist it should give an alert msg that table already exist if not it should go to second.aspx.I have written the code using <list string=""> i can able to show whether the table is exist or not but when iam trying to redirect to another page the code i have written for table existing was not working...
Here is my Code......`

enter code here

protected void Page_Load(object sender, EventArgs e)
        {

        }

 public Array GetTableName()
        {
            List<string> result = new List<string>();
            SqlCommand cmd = new SqlCommand("SELECT name FROM sys.Tables", con);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
                result.Add(reader["name"].ToString());
            return result.ToArray();
        }
 protected void Button3_Click(object sender, EventArgs e)
        {
            var tablenames = GetTableName();
            string Txttblname = txtdatabasetable.Text;

            foreach (var tablename in tablenames)
            {
                if (tablename.ToString() == Txttblname)
                {
                    label1.Text = "This Table Name Already Exist ";

                }
                else if (tablename.ToString() != Txttblname)
                {

                   Session["view1"] = txtdatabasetable.Text;

                   Response.Redirect("CreateTablePage.aspx?value=3");
                }
            }
        }

推荐答案

这有点不明显:您第一次绕过循环,或者您的第二次循环总是去Response .Redirect-因为您是第一次找到未找到您要查找的表的表,它将重定向.因此,如果第一个表名称匹配,则将设置标签文本,然后继续循环.在第二次循环中,表名将不匹配(因为它是第一次),因此它将重定向.
如果第一个不匹配,则它将立即重定向.

您需要对其进行更改,以便将变量设置为不匹配",并在数组中找到表名时将其设置为"match".您可以在循环后检查变量并确定要执行的操作.

另外:
1)您不需要检查
It''s a little obvious: Your first time round the the loop or your second will always go to Response .Redirect - because the first time you find a table that isn''t called what you are looking for, it will redirect. So if the first table name matches, then you will set the label text, then continue round the loop. On the second time round the loop, the table name will not match (because it did the first time) so it will Redirect.
If the first one doesn''t match, then it will redirect immediately.

You need to change it so that you set a variable to "doesn''t match" and set it to "match" when you find the table name in teh array. You tehn check the variable after the loop and decide what to do.

In addition:
1) You don''t need to check
if (condition)
   ...
else if (!condition)
   ...

因为else已覆盖!condition部分.
2)您不应该如此使用Array或var-请使用List< T>或IEnummerable T反而.它将使您的代码更具可读性.
3)您根本不需要这样做!只需在SELECT语句中包含WHERE子句即可仅返回具有您要查找的名称的表...

Because the else already covers the !condition part.
2) You shouldn''t use Array, or var so liberally - use List<T> or IEnummerable<T> instead. It will make you code much more readable.
3) You don''t need to do that at all! Just include a WHERE clause in your SELECT statement to return only tables with the name you are looking for...


调用此方法:
我现在只为你写rght!:)
call this method:
I write it rght now only for you!:)
public bool IntializingServer(string serverTable, string serverConnection )
        {
            var serverconn = new SqlConnection(serverConnection);
            serverconn.Open();
            var cmdtable = new SqlCommand("if EXISTS (select * from INFORMATION_SCHEMA.tables where table_name = 'Orders') select 'true'else select 'false'",serverconn);
            var dr = cmdtable.ExecuteReader();
                if(dr.GetString(4) == "true")
                {
                    return true;
                }
}


这篇关于使用c#查找给定表是否存在于数据库中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆