添加一条记录(100分)

  • 主题发起人 主题发起人 千中元
  • 开始时间 开始时间

千中元

Unregistered / Unconfirmed
GUEST, unregistred user!
连接都错了。。。
代码如下:
代码:
           string strConnect = ConfigurationSettings.AppSettings["DsnUserList"];
           string userName = tbUserName.Text.Trim();
           string Sex = ddlSex.SelectedItem.Text.Trim();
           int    Age =Int32.Parse(this.tbAge.Text.Trim());	
	string Email = tbEmail.Text.Trim();
           string strSQL = "SELECT TOP 1 * FROM USER";
try
   {
	  // create a new Connection object
   OleDbConnection objConnect = new OleDbConnection(strConnect);
				
				// open the connection to the database
    //  objConnect.Open();
       OleDbCommand selectCMD = new OleDbCommand(strSQL, objConnect);
                
       selectCMD.CommandTimeout = 30;
        OleDbDataAdapter custDA = new OleDbDataAdapter();
        custDA.SelectCommand = selectCMD;
        DataSet custDS = new DataSet();
        custDA.Fill(custDS,"USER");
        DataRow myNewRow;
        myNewRow = custDS.Tables["USER"].NewRow();
        myNewRow["UserName"] = userName;
        myNewRow["UserSex"] = Sex;
        myNewRow["UserAge"] = Age;
        myNewRow["UserEmail"] = Email;
        custDS.Tables["USER"].Rows.Add(myNewRow);
        custDS.AcceptChanges();
         custDA.Update(custDS);
				}
catch (Exception objError)
     {
				
	Label6.Text="错误"+objError.Source;
	Label7.Text=strConnect;
			
	return;			// and stop execution
}
Label6和Label7显示的错误信息:
错误Microsoft OLE DB Provider for SQL Server
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=psyTest;Data Source=QIANQIAN;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=QIANQIAN;
Use Encryption for Data=False;Tag with column collation when possible=False
问题是这个连接,我用Delphi的AdoConnection试过是可以的。
 
你所写的是ADO的连接。
.NET的数据连接是ADO.NET,已经和ADO不通了,ADO.net不是实时的连接,详细的情况请参考
http://www.able-consulting.com/ADO_Conn.htm
 
很奇怪,为什么你不用SqlConnection而用OleDbConnection?
m$是推荐用SqlConnection
 
try
{
OleDbConnection conn =new OleDbConnection("Provider=sqloledb;Data Source=192.20.2.4;Initial Catalog=kalstest;User Id=kals;Password=kals");
conn.Open();
this.textBox1.Text ="连接上";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
 
SqlConnection myConnection = new SqlConnection(myConnectionString);
string myInsertQuery = "INSERT INTO USER (USERNAME, USERSEX, USERAGE,USEREMAIL) Values(userName, Sex,Age,Email)";
SqlCommand myCommand = new SqlCommand(myInsertQuery);
myCommand.Connection = myConnection;
-------[brown]出错[/brown]
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
错误信息(objError.Source):
.Net SqlClient Data Provider

 
你可能装的是老的.net吧?
升级到新的看一下
 
是正式版本阿
 
接受答案了.
 
后退
顶部