服务端收到的数据为什么不能存入数据库?

已答复 服务端收到的数据为什么不能存入数据库?

  • 2009年4月2日 9:20
     
     

    我写的部分代码(测试用):
    private void serverBut_Click(object sender, EventArgs e)
            {
                IPHostEntry ipInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress ipAddress = ipInfo.AddressList[0];
                TcpListener TL = new TcpListener(ipAddress, 8800);
                TL.Start();

                TcpClient client = TL.AcceptTcpClient();
                NetworkStream NS = client.GetStream();
                IFormatter formatter = new BinaryFormatter();
                user.user newuser = (user.user)formatter.Deserialize(NS);
                //MessageBox.Show(newuser.Username);
                SqlConnection sqlcon = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
                sqlcon.Open();
                string InsertStr = "insert into User (userID,userName,userTel,userAddress) values('" + newuser.Userid + "','" + newuser.Username + "','" + newuser.Usertel + "','"  + newuser.Useraddress + "')";
                SqlCommand sqlcomm = new SqlCommand(InsertStr, sqlcon);
                sqlcomm.ExecuteNonQuery();
                sqlcon.Close();
                NS.Close();
                TL.Stop();
               
            }
    当接收到客户端提交的数据后,程序就会报错,提示在:sqlcomm.ExecuteNonQuery();处出错。我想可能是无法将数据存储到数据表中。但我不知道要如何才能正确地将服务端收到的程序存入数据表中,或是要用代理层来实现?
    第二个问题就是如何保持服务端程序运行一次后(也就是启动服务后)不关闭它,它就能自动侦听和接收客户端提交的数据并作出相应地处理?这是不是涉及到windows服务才能实现?
    请老师们帮忙解答!谢谢了。。。。

全部回复

  • 2009年4月2日 9:45
    版主
     
     
    出错信息?当时InsertStr的值

    windows服务应该可以
    http://feiyun0112.cnblogs.com/
  • 2009年4月2日 9:48
     
     已答复
    1.需要提供你报错的错误信息。
    2.可以使用web service 或者 .NET remoting 来实现。

    ps,你的sql不规范,应该使用参数方式。


    family as water
  • 2009年4月2日 12:37
    版主
     
     已答复
    string InsertStr = "insert into [User] (userID,userName,userTel,userAddress) values('" + newuser.Userid + "','" + newuser.Username + "','" + newuser.Usertel + "','"  + newuser.Useraddress + "')";

    另外检查字段类型是否正确,表里是否还有必填字段没有赋值的


    孟宪会
  • 2009年4月3日 1:25
     
     

    谢谢您们的热情解答,我已经按照“STone Z”提示修改了数据存储方式,就是以存储过程调用来实现,以参数的方式来加入字段就可以。可能真的是我之前这种以普通命令方式有地方出错了。