写了一个  用来向数据库插入数据的COM+  组件,所有步骤都执行到了,,执行成功了,没暴任何异常,  
但是数据就是不能写入数据库,无论是存储过程,还是普通的SQL语句,只要是写在组件里,它能执行成功,能返回值,但是没有数据写入数据库。  
让我非常郁闷啊。。  
同样的代码写在页面上它就能插入。
那位高手知道原因,或遇到这种情况,解决了的。请赐教啊。
附上一个简单的源码示例
这是组件
using System;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Data.SqlTypes;
using System.EnterpriseServices;
using System.Windows.Forms;
using System.Runtime.InteropServices;[assembly:ApplicationName("shiyong")]
[assembly:AssemblyKeyFileAttribute("regist.snk")]
namespace insert
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
[Transaction(TransactionOption.Required)]
public class Class1:ServicedComponent
{
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public int regedit(string ip,string data,string dataid,string datapwd,string cusid,string cuspwd)
{
System.Console.WriteLine("yunsi"+ip+","+data+","+dataid+","+datapwd+","+cusid+","+cuspwd);
string strsqlconnection ="server='"+ip+"';database='"+data+"';user id='"+dataid+"';password='"+datapwd+"';";
int result;
SqlConnection con =new SqlConnection(strsqlconnection);
string si="insert into login(uid,pwd) values('"+cusid+"','"+cuspwd+"')";
SqlCommand sqlcommand=new SqlCommand(si,con);
sqlcommand.Connection.Open();
result=sqlcommand.ExecuteNonQuery();
sqlcommand.Connection.Close();
return result;

}
}这是远程对象using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using insert;namespace remote
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1:System.MarshalByRefObject
{
public Class1()
{ }
public int regist(string ip,string data,string dataid,string datapwd,string cusid,string cuspwd)
{
   int result;
insert.Class1 i =new insert.Class1();
result=i.regedit(ip,data,dataid,datapwd,cusid,cuspwd);
return result;
}
}
}这是服务器
using System;
using System.Runtime;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using remote;namespace server
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
TcpServerChannel channel =new TcpServerChannel(3001);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(remote.Class1),"login",WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Heartland系统服务器");
System.Console.ReadLine();
}
}
}
这是桌面程序
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;
using System.Runtime.Remoting;
using remote;namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txt1;
private System.Windows.Forms.TextBox txt2;
private System.Windows.Forms.TextBox txt3;
private System.Windows.Forms.TextBox txt4;
private System.Windows.Forms.TextBox txt5;
private System.Windows.Forms.TextBox txt6;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txt1 = new System.Windows.Forms.TextBox();
this.txt2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.txt3 = new System.Windows.Forms.TextBox();
this.txt4 = new System.Windows.Forms.TextBox();
this.txt5 = new System.Windows.Forms.TextBox();
this.txt6 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
// 
// txt1
// 
this.txt1.Location = new System.Drawing.Point(144, 24);
this.txt1.Name = "txt1";
this.txt1.TabIndex = 0;
this.txt1.Text = "";
// 
// txt2
// 
this.txt2.Location = new System.Drawing.Point(144, 64);
this.txt2.Name = "txt2";
this.txt2.TabIndex = 1;
this.txt2.Text = "";
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(160, 264);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "but1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// txt3
// 
this.txt3.Location = new System.Drawing.Point(144, 104);
this.txt3.Name = "txt3";
this.txt3.TabIndex = 3;
this.txt3.Text = "";
// 
// txt4
// 
this.txt4.Location = new System.Drawing.Point(144, 144);
this.txt4.Name = "txt4";
this.txt4.TabIndex = 4;
this.txt4.Text = "";
// 
// txt5
// 
this.txt5.Location = new System.Drawing.Point(144, 184);
this.txt5.Name = "txt5";
this.txt5.TabIndex = 5;
this.txt5.Text = "";
// 
// txt6
// 
this.txt6.Location = new System.Drawing.Point(144, 224);
this.txt6.Name = "txt6";
this.txt6.TabIndex = 6;
this.txt6.Text = "";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 341);
this.Controls.Add(this.txt6);
this.Controls.Add(this.txt5);
this.Controls.Add(this.txt4);
this.Controls.Add(this.txt3);
this.Controls.Add(this.button1);
this.Controls.Add(this.txt2);
this.Controls.Add(this.txt1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
int tt;
string ip,data,dataid,datapwd,cusid,cuspwd;
ip=txt1.Text;
data=txt2.Text;
dataid=txt3.Text;
datapwd=txt4.Text;
cusid=txt5.Text;
cuspwd=txt6.Text;
MessageBox.Show("j"+ip+","+data+","+dataid+","+datapwd+","+cusid+","+cuspwd);
remote.Class1  register =(remote.Class1)Activator.GetObject(typeof(remote.Class1),"tcp://"+ip+":3001/login");
tt=register.regist(ip,data,dataid,datapwd,cusid,cuspwd);
if(tt==1)
{
MessageBox.Show("成功");
}
else
{
MessageBox.Show("失败");
} } }
}

解决方案 »

  1.   

    说一下。
    数据库我就在master里新建了login表,字段uid,pwd
    哪个有兴趣的帮忙看一下,,
    感激不尽啊
      

  2.   

    加一个try catch,看看有没有异常发生,并且如果IDBCommand.ExecuteNonQuery执行结果不等于-1也记录一下,看看原因.
      

  3.   

    你用sql server的profiler工具监视一下,看看sql命令是否传过去。
      

  4.   

    to 你用sql server的profiler工具监视一下sql server自带的,不需要下载,我给的是英文名,不知道中文叫什么
      

  5.   

    试试
    [AutoComplete]
    public int regedit(string ip,string data,string dataid,string datapwd,string cusid,string cuspwd)
    {
    System.Console.WriteLine("yunsi"+ip+","+data+","+dataid+","+datapwd+","+cusid+","+cuspwd);
      

  6.   

    谢谢了,可以了。
    能否解释一下,[AutoComplete] 这个有什么作用?
      

  7.   

    倒,,,,
    晕死。
    我忘了。 
    是这个东东在搞鬼吧[Transaction(TransactionOption.Required)]
    以前写这个的时候我是准备写事务的,还只写了插入语句的时候,运行时看不能插入数据,就一直找原因,把这个给忘到九霄云外了。
    真的谢了啊
      

  8.   

    在某些方法前面加上 <AutoComplete()> 属性很重要。如有需要,COM+ 上下文将提交当前事务,并且在方法返回时停用该对象。