ASP.NET中的数据库连接 [英] Database connection in ASP.NET

查看:68
本文介绍了ASP.NET中的数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个带有文本框和按钮的webpate.

如果我们输入表名并按下按钮,则应显示该表是否存在.

我要代码检查数据表是否存在.....

Hi,

I have created a webpate with a textbox and a button.

If we enter the tablename and press button it should show whether the table exists or not.

I want code to check whether the table exists in the databe or not.....

推荐答案

拉维,
获取数据库中所有表的代码如下.
Hi ravi,
code to get all the tables in your database is as follows.
USE YourDBName
GO 
SELECT *
FROM sys.Tables
GO


然后,您可以实现您的逻辑.
如果无法获取,请向我发送查询.


Then you can implement your logic.
If you unable to get, send me a query.


您需要触发查询并检查数据库中是否存在表

You need to fire query and check if table is exist in database

IF EXISTS(SELECT * FROM SYSOBJECTS WHERE name = 'tablename' AND xtype = 'U')


在按钮上使用此按钮单击

Use this on Button Click

SqlConnection conn = new SqlConnection();
       conn.ConnectionString = "Ur Connection String";
       conn.Open();
       string strCommand = "SELECT * FROM INFORMATION_SCHEMA.TABLES where table_type!=''View'' and table_name!=''sysdiagrams''";
       SqlCommand cmd = new SqlCommand(strCommand, conn);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataTable dt = new DataTable();
       da.Fill(dt);

       foreach (DataRow dr in dt.Rows)
       {
           if (dr[2] != YourTextBox.Text)
           {
               Response.Write("Table not exist");
           }
       }

       cmd.Dispose();
       conn.Close();


这篇关于ASP.NET中的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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