Console.WriteLine("#rows after change:{0}", thisDataSet.Tables["ACustomers"].Rows.Count); thisConnection.Close(); Console.WriteLine("Program finished,press any key to continue:"); Console.ReadKey(); } } }这是下午写的。楼主可以参考一下,最好系统的学下ADO.NET
如果返回0就是没有哟 如果有值就存在哦(@"SELECT count(*) FROM sys.objects WHERE (object_id = OBJECT_ID(N'[dbo].["+ tableName+"]')) AND (type in (N'U'))");
SELECT * FROM 库名..SYSOBJECTS WHERE ID=OBJECT_ID('T')
begin
endhttp://topic.csdn.net/u/20091109/18/c5903d8d-fe18-4599-acf2-fc3cd8f53d6f.html
string str,tableName;
tableName = "1用户";
str = myDb.GetValue(@"SELECT * FROM sys.objects WHERE (object_id = OBJECT_ID(N'[dbo].[" + tableName + "]')) AND (type in (N'U'))");
Console.WriteLine(!string.IsNullOrEmpty(str));
Console.ReadLine();
自己找个操作SQL的类: protected SqlConnection conn; //创建连接对象
protected SqlDataAdapter da; //创建适配器
private string connString ;//连接字符串 public DataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
connString = @"Data Source=上天下地无所不能的笨笨;Initial Catalog=木有数据库;Integrated Security=True";
} #region 数据库连接控制
//打开数据库连接
protected void Open()
{
try
{
if (conn == null)
{
conn = new SqlConnection(connString);
}
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
}
catch(Exception e)
{
// MessageBox.Show(e.Message);
}
} //关闭数据库连接
protected void Close()
{
if (conn != null)
conn.Close();
} //释放数据库连接资源
protected void Dispose()
{
// 确认连接是否已经关闭
if (conn != null)
{
conn.Dispose();
conn = null;
}
}
#endregion
#region 执行SQL 返回单值实现 public string GetValue(string SqlStr)
{
string Sql = SqlStr; this.Open();
SqlCommand cmd = new SqlCommand(Sql, conn);
try
{
if (conn.State == ConnectionState.Open)
{
Sql = Convert.ToString(cmd.ExecuteScalar());
}
}
catch (SqlException e)
{
//MessageBox.Show(e.Message);
}
this.Dispose();
this.Close();
return Sql;
} #endregion
DataBase myDb = new DataBase();
string str,tableName;
tableName = "1用户";
str = myDb.GetValue(@"SELECT * FROM sys.objects WHERE (object_id = OBJECT_ID(N'[dbo].[" + tableName + "]')) AND (type in (N'U'))");
Console.WriteLine(!string.IsNullOrEmpty(str));
Console.ReadLine();
我的VS2008里面没有DataBase这个类啊,是不是需要添加什么库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;namespace MyDemo
{
class Program
{
static void Main(string[] args)
{
//连接字符串
string connectString =
@"server=JIAANGFENG-PC\SQLEXPRESS;database=Northwind;Integrated Security=SSPI";
SqlConnection thisConnection = new SqlConnection(connectString);
//查询字符串
string selectCommand="SELECT CustomerID,CompanyName FROM Customers";
try
{
thisConnection.Open();
Console.WriteLine("Opened Connection to {0}",
thisConnection.ConnectionString);
}
catch
{
Console.WriteLine("false");
}
//使用 SelectCommand 和 SqlConnection 对象初始化 SqlDataAdapter 类的一个新实例。
SqlDataAdapter thisAdapter = new SqlDataAdapter(selectCommand, thisConnection);
/* SqlDataAdapter 不会自动生成实现 DataSet 的更改
* 与关联的 SQL Server 实例之间的协调所需的
* Transact-SQL 语句。但是,如果设置了 SqlDataAdapter
* 的 SelectCommand 属性,则可以创建一个 SqlCommandBuilder
* 对象来自动生成用于单表更新的 Transact-SQL 语句。
* 然后,SqlCommandBuilder 将生成其他任何未设置的 Transact-SQL 语句。*/
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter); DataSet thisDataSet = new DataSet();
//在 DataSet 中添加或刷新行以匹配使用 DataSet 和 DataTable 名称的数据源中的行。
// DataTable 名称的数据源中的行:是否是数据库的表名?不是,可以任意取。
thisAdapter.Fill(thisDataSet, "ACustomers"); Console.WriteLine("# rows before change:{0}",
thisDataSet.Tables["ACustomers"].Rows.Count); /*为了识别表中的记录,表的主键必须唯一。
* 表的主键还可以由两列或多列组成。这在单列无法
* 包含足够的唯一值时发生。例如,两列主键可由“FirstName”
* 和“LastName”列组成。因为主键可由多列组成,所以 PrimaryKey
* 属性由 DataColumn 对象的数组组成。
*/
//设置列主键,数组只有一个元素表示主键只有一个。
DataColumn[] keys = new DataColumn[1];
keys[0] = thisDataSet.Tables["ACustomers"].Columns["CustomerID"];
thisDataSet.Tables["ACustomers"].PrimaryKey = keys; //获取由主键值指定的行。为空则返回null
//错误:未将对象引用设置到对象的实例。
DataRow findRow = thisDataSet.Tables["ACustomers"].Rows.Find("zhji"); if (findRow == null)
{
Console.WriteLine("zhen not found,will add to Customers table"); //创建与该表具有相同架构的新 DataRow。
DataRow thisRow = thisDataSet.Tables["ACustomers"].NewRow();
thisRow["CustomerID"] = "zhji";
thisRow["CompanyName"] = "fengtian Ltd";
thisDataSet.Tables["ACustomers"].Rows.Add(thisRow);
if ((findRow = thisDataSet.Tables["ACustomers"].Rows.Find("zhji")) != null)
{
Console.WriteLine("zhji successfully added to Customerstable");
}
}
else
{
Console.WriteLine("zhji already pressent in database");
} thisAdapter.Update(thisDataSet, "ACustomers");
Console.WriteLine("#rows after change:{0}",
thisDataSet.Tables["ACustomers"].Rows.Count); thisConnection.Close();
Console.WriteLine("Program finished,press any key to continue:");
Console.ReadKey();
}
}
}这是下午写的。楼主可以参考一下,最好系统的学下ADO.NET
如果有值就存在哦(@"SELECT count(*) FROM sys.objects WHERE (object_id = OBJECT_ID(N'[dbo].["+ tableName+"]')) AND (type in (N'U'))");
{
select ...
}
string strMysql="show tables"执行上面的2行SQL语句,检查返回值