日期:2014-05-19  浏览次数:20853 次

c#.net环境中引入SQL Server???
我想用 "打开 "菜单直接打开一个SQL   Server数据库,方法如下:
OpenFileDialog   openFileDialog1=new   OpenFileDialog   ();
openFileDialog1.InitialDirectory   = "c:\\ ";
openFileDialog1.Filter   = "*.mdf|*.txt|All   files(*.*)|*.* ";
m_filename=openFileDialog1.FileName   ;
datagrid1.DataSource   =Read(m_filename   );
public   object   Read(string   file)
{
    string     myconn= "Integrated   Security=SSPI;User
id=sa;database=file;server=localhost ";
  SqlConnection   myconnection=new   SqlConnection   (myconn);
......
}
运行时打开对话框中选择扩展名为.mdf的文件,提示 "无法登录file中的数据库 ",这是为什么
还有,运行时打开数据库后我想选择数据库中的某一个表,该如何操作让该数据库中的表给显示出来.因为我想让某一个表的内容显示在datagrid1中.
总之,就是在窗体运行时,从菜单中选择某一个数据库,再选择数据库中的一个表,让它显示在datagrid中,该如何实现呢?
先谢谢各位了



------解决方案--------------------


DataSet ds = new DataSet();
string cmd = "select 服务器名称,价格,数量,收购商名称 from serverInfo where 服务器名称 in (select Own_Server from Info_Own) ";
ds = DB.ExecuteSqlDS(cmd, "serverInfo ");
this.imageShow.DataSource = ds.Tables[ "serverInfo "];

DB中函数:
public static DataSet ExecuteSqlDS(string strSQL,string tablename)
{
SqlConnection conn = new SqlConnection(strConn);
try
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSQL, conn);
DataSet ds = new DataSet( "ds ");
sda.Fill(ds,tablename); //调用SqlDataAdapter的Fill方法,为DataSet填充数据
return ds; //返回得到的DataSet对象,保存了从数据库查询到的数据
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
finally
{
conn.Close();
}
}
------解决方案--------------------
SELECT name FROM sysobjects WHERE xtype = 'U ' AND (Not (id) = 133575514)
把表名称填到datagrid1中,然后就抓到NAME值,再写SQL