帮你的连接数据库的sql语句写出来看看?

解决方案 »

  1.   

    把你的链接数据库的SQL语句贴出来看看
      

  2.   

    没有。我是先放了一个sqldataadapter控件,然后,系统提示开始链接数据库,我找到了服务器名(CWQ),并找到了我的数据库,然后,系统自动生成了一个sqlconnection1,然后,我在数据菜单中选择生成数据集,取名为cwqdata,然后将datagrid控件与他们进行捆绑。
    在代码中只写了 me.sqldataadapter1.fill(cwqdata)
              me.databind()然后运行时,它总提示:用cwq\aspnet登录失败。
      

  3.   

    其实我觉得这类问题,要不出在你的连接数据库的语句上,要不出在你数据库上,你必须保证你可以可信任连接上SQL。
      

  4.   

    我放置sqldataadapter控件并且链接数据库时,进行了测试,能够成功链接到我的数据库上。但是,在代码中就是出错,不知道为什么?为什么会提示cwq\aspnet登录失败呢?
      

  5.   

    其实有多年方法的,你可以用sqldataadapter他的属性,也可用代码,你要是要的话我发给你
      

  6.   

    你在代码里看看这个行不行
    public void getconn()
    {
    try
    {
    SqlDataAdapter myad = new SqlDataAdapter();
    SqlConnection mycon = new SqlConnection("data source = JSJ227\\SWFCCID ;"
    +"initial catalog=dgl;"
    +"persist security info=False;"
    +"user id=sa;workstation id=JSJ227;"
    +"packet size=4096");
    DataSet  myset = new DataSet();
    string textcom="SELECT * FROM  学生成绩表$";  myad.SelectCommand = new SqlCommand(textcom,mycon); myad.Fill( myset,"学生成绩表$"); dataGrid1.DataSource=myset.Tables["学生成绩表$"].DefaultView ;
    dataGrid1.Refresh();}
    catch( Exception rc)
    {
    MessageBox.Show("错误" + rc.Message );
    }
      

  7.   

    我还有一个asp.net连接数据库的专用的组件你要不要,只能给你代码了,但是你执行了以后,把里面的.dll文件引用的你的工程里会很方便的
      

  8.   

    我还是给你发个类吧,用c#写的类专用来连接SQL数据库的using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Security.Cryptography; //使用RSA加密解密类
    using System.Windows.Forms;
    using System.IO;
    using System.Text;namespace ReadPubs
    {
    /// <summary>
    /// DataOperate 的摘要说明。
    /// </summary>
    public class DataOperate
    {
    public static string StrConn;  //申明通用数据库连接字符串 公用变量
    private System.Data.SqlClient.SqlConnection sqlConnection;
    private System.Data.SqlClient.SqlCommand sqlCommand;
    private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter;
    private System.Data.SqlClient.SqlDataReader sqlDataReader=null;
    private System.Data.DataSet dataSet;
    private System.Data.DataTable dataTable=null; private RSACryptoServiceProvider MyRSA=new RSACryptoServiceProvider();

    public string SetStrConn
    {
    get
    {
    return StrConn;
    }
    set
    {
    StrConn=value;
    }
    } public DataOperate()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    sqlConnection=new SqlConnection(StrConn);
    } public DataOperate(string ConnStr)
    {
    StrConn=ConnStr;
    sqlConnection=new SqlConnection(StrConn);
    }
    public DataOperate(string ConnDBPath,bool ToStrConn)
    {
    if(ToStrConn)
    StrConn=GetConnStr(ConnDBPath);
    sqlConnection=new SqlConnection(GetConnStr(ConnDBPath));
    } public void ExecuteSqlCommand(string StrSql)  //StrSql为Insert、Delete、Update等SQL语句
    { //执行传入的SQL语句
    try
    {
    sqlConnection.Open();
    sqlCommand=new SqlCommand(StrSql,sqlConnection);
    sqlCommand.ExecuteNonQuery();
    sqlCommand.Dispose();
    sqlConnection.Close();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    sqlConnection.Close();
    }
    } public DataTable ExecuteTable(string StrSql)//执行传入的Select SQL语句返回数据表
    {
    try
    {
    sqlConnection.Open();
    sqlDataAdapter=new SqlDataAdapter(StrSql,sqlConnection);
    dataSet=new DataSet();
    sqlDataAdapter.Fill(dataSet,"StrTable");
    dataTable=dataSet.Tables["StrTable"];
    //dataSet.Clear();
    sqlDataAdapter.Dispose();
    sqlConnection.Close();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    sqlConnection.Close();
    }
    return dataTable;
    }

    public SqlDataReader SqlReader(string StrSql)
    {
    SqlDataReader sqlReader=null;
    try
    {
    sqlConnection.Open();
    sqlCommand=new SqlCommand(StrSql,sqlConnection);
    sqlReader=sqlCommand.ExecuteReader();
    sqlCommand.Dispose();
    sqlConnection.Close();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    sqlConnection.Close();
    }
    return sqlReader;//调用后要注间关闭读者对象
    }
    public DataSet ExecuteDataSet(string StrSql,string StrTableName)//执行传入的Select SQL语句返回DataSet
    { //StrTableName为DataSet中的表名
    dataSet=new DataSet();
    try
    {
    sqlConnection.Open();
    sqlDataAdapter=new SqlDataAdapter(StrSql,sqlConnection);
    //dataSet.Tables.Add(StrTableName);
    //sqlDataAdapter.Fill(dataSet,StrTableName);
    sqlDataAdapter.Fill(dataSet);
    sqlDataAdapter.Dispose();
    sqlConnection.Close();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    sqlConnection.Close();
    }
    return dataSet;
    }
    public int GetMaxNum(string StrSql)
    {
    int MaxNumber=0;
    try
    {
    sqlConnection.Open();
    sqlCommand=new SqlCommand(StrSql,sqlConnection);
    sqlDataReader=sqlCommand.ExecuteReader();
    if(sqlDataReader.Read()) 
    {
    if(sqlDataReader[0].ToString()!="")
    MaxNumber=(int)sqlDataReader[0];
    }
    /*else
    {
    MaxNumber="0001";
    }*/
    sqlCommand.Dispose();
    sqlDataReader.Close();
    sqlConnection.Close();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    sqlConnection.Close();
    }
    return MaxNumber;
    }
    public int GetRecordCount(string StrSql)//执行传入的Select SQL语句得到记录总数
    {
    int recordCount=0;
    try
    {
    recordCount=ExecuteTable(StrSql).Rows.Count;
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }
    return recordCount;
    }
    public string GetConnStr(string StrDBPath)
    {
    string MyStrConn=null;
    string TempReadLine=null;
    try
    {
    Stream SReadLine=File.OpenRead(StrDBPath);
    StreamReader SrReadLine=new StreamReader(SReadLine,System.Text.Encoding.ASCII);
    SrReadLine.BaseStream.Seek(0,SeekOrigin.Begin); while(SrReadLine.Peek()>-1)
    {
    TempReadLine=SrReadLine.ReadLine();
    if(TempReadLine.EndsWith(";"))
    MyStrConn+=TempReadLine;
    else
    MyStrConn+=TempReadLine+";";
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }
    return MyStrConn;
    } public string EncryptString(string StrSend)
    {
    byte[] StrTxt=Encoding.Unicode.GetBytes(StrSend.ToCharArray());
    byte[] MyByte=MyRSA.Encrypt(StrTxt,false);
    return Encoding.Unicode.GetString(MyByte);
    } public string DecryptString(string StrSend)
    {
    byte[] StrTxt=Encoding.Unicode.GetBytes(StrSend.ToCharArray());
    byte[] MyByte=MyRSA.Decrypt(StrTxt,false);
    return Encoding.Unicode.GetString(MyByte);
    }
    }
    }
      

  9.   

    我还有一个VB.net写的类,你要是要的话,给我说一下,我的QQ是93644364
    那个组件就不给你了,那代码太长了,
      

  10.   

    是SQL SERVER 的问题,在SQL SERVER中没有cwq\aspnet这个用户,添加一个
    就OK了!
      

  11.   

    boulder(day day up!) 说的对,
    在SQL SERVER企业管理器的安全性结点的登录里添加一个cwq\aspnet用户并设置权限就OK了,
      

  12.   

    重新作一次,记得在向导连接数据库输入密码的时候选择"允许保存密码"要不在修改sqlconnection1的ConnectionString,z在里面添加用户名和密码!!