SqlConnection Conn = new SqlConnection("server=localhost; uid=sa;pwd=;database=dbname" );
Conn.Open();

解决方案 »

  1.   

    namespace HowTo.Samples.ADONET
    {using System;
    using System.Data;
    using System.Data.OleDb;public class adodtreader
    {
      public static void Main()
      {
        adodtreader myadodtreader = new adodtreader();
        myadodtreader.Run();
      }  public void Run()
      {
        OleDbDataReader myDataReader = null;    OleDbConnection myOleDbConnection = new OleDbConnection("server=(local)\\NetSDK;Trusted_Connection=yes;database=Northwind;provider=sqloledb");
        OleDbCommand myOleDbCommand = new OleDbCommand("SELECT EmployeeID, LastName, FirstName, Title, ReportsTo FROM Employees", myOleDbConnection);    try
        {
          myOleDbConnection.Open();
          myDataReader = myOleDbCommand.ExecuteReader();      Console.Write("EmployeeID" + "\t");
          Console.Write("Name" + "\t");
          Console.Write("Title" + "\t");
          Console.Write("ReportsTo" + "\n");      // Always call Read before accessing data.
          while (myDataReader.Read())
          {
            Console.Write(myDataReader.GetInt32(0) + "\t");
            Console.Write(myDataReader.GetString(2) + " " + myDataReader.GetString(1) + "\t");
            Console.Write(myDataReader.GetString(3) + "\t");
            if (myDataReader.IsDBNull(4))
              Console.Write("无法使用\n");
            else
              Console.Write(myDataReader.GetInt32(4) + "\n");
          }
        }
        catch(Exception e)
        {
          Console.Write(e.ToString());
        }
        finally
        {
          // Always call Close when done reading.
          if (myDataReader != null)
            myDataReader.Close();      // Close the connection when done with it.
          if (myOleDbConnection.State == ConnectionState.Open)
            myOleDbConnection.Close();
        }
      }
    }}
      

  2.   

    namespace HowTo.Samples.ADONET
    {using System;
    using System.Data;
    using System.Data.SqlClient;public class gettingdata
    {
      public static void Main()
      {
        gettingdata mygettingdata = new gettingdata();
        mygettingdata.Run();
      }  public void Run()
      {    SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;Trusted_Connection=yes;database=northwind");
        SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("select * from customers", myConnection);    try
        {
          DataSet myDataSet = new DataSet();      mySqlDataAdapter.Fill(myDataSet,"Customers");      foreach (DataRow myDataRow in myDataSet.Tables["Customers"].Rows)
          {
            Console.WriteLine(myDataRow["CustomerId"].ToString());
          }
        }
        catch(Exception e)
        {
          Console.WriteLine(e.ToString());
        }
      }
    }}
      

  3.   

    private void btn登录_Click(object sender, System.EventArgs e)
     {
        String strcon;
        String strcom;
       SqlConnection Conn=new SqlConnection();
       SqlCommand Comm=new SqlCommand();
       SqlDataReader DR;
       strcon="server=dataserver;uid=sa;pwd=123456;database=temp";
       Conn.ConnectionString =strcon;
    strcom="select 用户密码 from 用户 where 用户登录名='" + this.txt用户名.Text.Trim() + "'";
      if (this.txt用户名.Text.Trim()=="") 
       {
     MessageBox.Show("请输入用户名!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Exclamation );
    }
    else if (this.txt密码.Text.Trim()=="")
      MessageBox.Show("请输入密码!","提 示",MessageBoxButtons.OK ,MessageBoxIcon.Exclamation );
    else 
    {
      Conn.Open();
      Comm.Connection =Conn;
      Comm.CommandText =strcom;
      DR=Comm.ExecuteReader();
      if (DR.Read()!=true)
    {
       MessageBox.Show("您输入的用户名不存在!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Exclamation );
    }
    else
    {
    if (DR.GetString(0)!=this.txt密码.Text.Trim())
    {
    MessageBox.Show("您输入密码错误!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Exclamation );
    }
    else
    {
    DR.Close();
    Conn.Close();
    MessageBox.Show("登录成功!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Exclamation );

    }
    }
    }


    }
      

  4.   

    namespace HowTo.Samples.ADONET
    {using System;
    using System.Data;
    using System.Data.SqlClient;public class filterdatatable
    {
      public static void Main()
      {
        filterdatatable myfilterdatatable = new filterdatatable();
        myfilterdatatable.Run();
      }  public void Run()
      {
        // Create a new Connection and SqlDataAdapter    SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;Trusted_Connection=yes;database=northwind");
        SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("select * from customers", myConnection);    try
        {
          // Create the new instance of the DataSet
          DataSet myDataSet = new DataSet();      // Load the customer table from the database into a table called Customers in the dataset
          mySqlDataAdapter.Fill(myDataSet,"Customers");      // Create a new dataview instance on the Customers table that was just created
          DataView myDataView = new DataView(myDataSet.Tables["Customers"]);      // Sort the view based on the FirstName column
          myDataView.Sort = "CustomerId";      // Filter the dataview to only show customers with the CustomerID of ALFKI
          myDataView.RowFilter = "CustomerID='ALFKI'";      for (int i = 0; i < myDataView.Count; i++)
          {
            Console.Write(myDataView[i]["CustomerId"].ToString() + " - " + myDataView[i]["CompanyName"].ToString());
          }
        }
        catch(Exception e)
        {
          Console.WriteLine(e.ToString());
        }
      }
    }}
      

  5.   

    namespace HowTo.Samples.ADONET
    {using System;
    using System.Data;
    using System.Data.SqlClient;public class filterdatatable
    {
      public static void Main()
      {
        filterdatatable myfilterdatatable = new filterdatatable();
        myfilterdatatable.Run();
      }  public void Run()
      {
        // Create a new Connection and SqlDataAdapter    SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;Trusted_Connection=yes;database=northwind");
        SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("select * from customers", myConnection);    try
        {
          // Create the new instance of the DataSet
          DataSet myDataSet = new DataSet();      // Load the customer table from the database into a table called Customers in the dataset
          mySqlDataAdapter.Fill(myDataSet,"Customers");      // Create a new dataview instance on the Customers table that was just created
          DataView myDataView = new DataView(myDataSet.Tables["Customers"]);      // Sort the view based on the FirstName column
          myDataView.Sort = "CustomerId";      // Filter the dataview to only show customers with the CustomerID of ALFKI
          myDataView.RowFilter = "CustomerID='ALFKI'";      for (int i = 0; i < myDataView.Count; i++)
          {
            Console.Write(myDataView[i]["CustomerId"].ToString() + " - " + myDataView[i]["CompanyName"].ToString());
          }
        }
        catch(Exception e)
        {
          Console.WriteLine(e.ToString());
        }
      }
    }}
      

  6.   

    to  sinsky(十方)你用的是sql数据库了?
    谢谢
    能把你的例子给我发送到邮箱里面吗?
      

  7.   

    和vb.net中是一样的,因为都是用的ADO.net,就语法不同而已。SqlConnection myConn = new SqlConnection("server=localhost; uid=sa;pwd=;database=pubs" );myConn.Open();
                                    ^^
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   ^^   
                               ~~~~~~~~~~~~~~~~~
         要学的实在太多,
            来不及自己摸索,
                不如互相帮助!    ^^
                                    
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    -by BigRongShu  
                                    (欢迎您和我探讨)
      

  8.   

    有例子请发到我的邮箱
    [email protected]
    谢谢
      

  9.   

    VS.Net里有QuickStart,那里有所有例子。Do it youself!!!