使用sql server 2005的winodws移动C#应用程序 [英] winodws mobile C# application using sql server 2005

查看:79
本文介绍了使用sql server 2005的winodws移动C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做我的毕业设计,我不知道该怎么做

我制作了应用程序,并且一切都很完美,只有一件事

使用lan或wiless lan的应用程序和sql server 2005之间的连接(在我的情况下,我正在wilesslan上进行测试,但我将需要lan 2进行连接)

我从代码项目中得到了一个类,并对其进行了一些修改:

代码:

****************************************************** **********************

i am doing my graduation project and i dont know what to do

i make the application and it all works perfect exept one thing

the connection between the application and the sql server 2005 using lan or wiless lan ( in my case i am testing on wilesslan but i will be needing it by lan 2)

i got a class from code project and modified it a little :

the code:

************************************************************************

using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Threading;<br />
using System.Net;<br />
using System.Net.Sockets;<br />
using System.Diagnostics;<br />
namespace TBMultiFunctionLibrary.ClientSocket<br />
{<br />
    public delegate void DataArrivalHandler(object o, DataArrivalEventArgs e);<br />
    public class DataArrivalEventArgs : EventArgs<br />
    {<br />
        public readonly string Message;<br />
        public DataArrivalEventArgs(string str)<br />
        {<br />
            Message = str;<br />
        }<br />
    }<br />
    public class ClientSocket<br />
    {<br />
        private TcpClient tcp = null;<br />
        private Thread receiveThread = null;<br />
        private byte[] received = null;<br />
        private byte[] dataToSend = null;<br />
        private System.Net.IPEndPoint ip;<br />
        public event DataArrivalHandler DataArrived;<br />
        public ClientSocket(int port)<br />
        {<br />
            tcp = new TcpClient((new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], port)));<br />
        }<br />
        public ClientSocket(string ipAddress, int port)<br />
        {<br />
            tcp = new TcpClient((new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], port)));<br />
            Connect(ipAddress, port);<br />
        }<br />
        public void Connect(string ipAddress, int port)<br />
        {<br />
            try<br />
            {<br />
                ip = new IPEndPoint(Dns.GetHostEntry(ipAddress).AddressList[0], port);<br />
                IPAddress address = IPAddress.Parse(ipAddress);<br />
                LingerOption lingerOption = new LingerOption(false, 1);<br />
                tcp.LingerState = lingerOption;<br />
                tcp.Connect(ip);<br />
                receiveThread = new Thread(new ThreadStart(ThreadProcReceive));<br />
                receiveThread.Name = "Client''s Receive Thread";<br />
                //receiveThread.SetApartmentState(ApartmentState.MTA);<br />
                receiveThread.Start();<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                //Connect(ipAddress, port);<br />
                throw ex;<br />
                //MessageBox.Show(ex.Message, "Connection failure", MessageBoxButtons.OK, MessageBoxIcon.Error);<br />
            }<br />
        }<br />
        private void ThreadProcReceive()<br />
        {<br />
            for (; ; )<br />
            {<br />
                received = new byte[1024];<br />
                int readBytes = 0;<br />
                try<br />
                {<br />
                    readBytes = tcp.GetStream().Read(received, 0, received.Length);<br />
                }<br />
                catch (Exception)<br />
                {<br />
                    return;<br />
                }<br />
                if (readBytes == 8)<br />
                {<br />
                    StringBuilder shutMessage = new StringBuilder(8);<br />
                    for (int count = 0; count < 8; count++)<br />
                    {<br />
                        char ch = (char)received[count];<br />
                        shutMessage = shutMessage.Append(ch);<br />
                    }<br />
                    string shut = "shutdown";<br />
                    string receivedMessage = shutMessage.ToString();<br />
                    if (receivedMessage.Equals(shut))<br />
                    {<br />
                        //MessageBox.Show(this,"Shutdown Request has arrived from the \nconnected party.\nYou cannot send message anymore.\nPlease close the window.","Shut Down Request",MessageBoxButtons.OK,MessageBoxIcon.Information);<br />
                        DataArrived(new object(), new DataArrivalEventArgs("shutdown"));<br />
                        return;<br />
                    }<br />
                }<br />
                StringBuilder str = new StringBuilder(1024);<br />
                for (int count = 0; count < readBytes; count++)<br />
                {<br />
                    char ch = (char)received[count];<br />
                    str = str.Append(ch);<br />
                    //str = str.Append(" ");<br />
                }<br />
                GC.Collect();<br />
                DataArrived(new object(), new DataArrivalEventArgs(str.ToString()));<br />
            }<br />
<br />
        }<br />
        public void sendData(string sendData)<br />
        {<br />
            if (sendData.Length != 0)<br />
            {<br />
                char[] charArray = sendData.ToCharArray();<br />
                dataToSend = new byte[sendData.Length];<br />
                for (int charCount = 0; charCount < sendData.Length; charCount++)<br />
                    dataToSend[charCount] = (byte)charArray[charCount];<br />
            }<br />
            else<br />
                dataToSend = new byte[] { (byte)''e'', (byte)''m'', (byte)''p'', (byte)''t'', (byte)''y'' };<br />
            try<br />
            {<br />
                tcp.GetStream().Write(dataToSend, 0, dataToSend.Length);<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                throw ex;<br />
                //Connect(myipAddress, myPort);<br />
            }<br />
            GC.Collect();<br />
        }<br />
        public void Disconnect()<br />
        {<br />
            dataToSend = new byte[] { (byte)''s'', (byte)''h'', (byte)''u'', (byte)''t'', (byte)''d'', (byte)''o'', (byte)''w'', (byte)''n'' };<br />
            tcp.GetStream().Write(dataToSend, 0, dataToSend.Length);<br />
            tcp.Close();<br />
        }<br />
    }<br />
}<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Threading;<br />
using System.Net;<br />
using System.Net.Sockets;<br />
using System.Diagnostics;<br />
namespace TBMultiFunctionLibrary.ClientSocket<br />
{<br />
    public delegate void DataArrivalHandler(object o, DataArrivalEventArgs e);<br />
    public class DataArrivalEventArgs : EventArgs<br />
    {<br />
        public readonly string Message;<br />
        public DataArrivalEventArgs(string str)<br />
        {<br />
            Message = str;<br />
        }<br />
    }<br />
    public class ClientSocket<br />
    {<br />
        private TcpClient tcp = null;<br />
        private Thread receiveThread = null;<br />
        private byte[] received = null;<br />
        private byte[] dataToSend = null;<br />
        private System.Net.IPEndPoint ip;<br />
        public event DataArrivalHandler DataArrived;<br />
        public ClientSocket(int port)<br />
        {<br />
            tcp = new TcpClient((new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], port)));<br />
        }<br />
        public ClientSocket(string ipAddress, int port)<br />
        {<br />
            tcp = new TcpClient((new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], port)));<br />
            Connect(ipAddress, port);<br />
        }<br />
        public void Connect(string ipAddress, int port)<br />
        {<br />
            try<br />
            {<br />
                ip = new IPEndPoint(Dns.GetHostEntry(ipAddress).AddressList[0], port);<br />
                IPAddress address = IPAddress.Parse(ipAddress);<br />
                LingerOption lingerOption = new LingerOption(false, 1);<br />
                tcp.LingerState = lingerOption;<br />
                tcp.Connect(ip);<br />
                receiveThread = new Thread(new ThreadStart(ThreadProcReceive));<br />
                receiveThread.Name = "Client''s Receive Thread";<br />
                //receiveThread.SetApartmentState(ApartmentState.MTA);<br />
                receiveThread.Start();<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                //Connect(ipAddress, port);<br />
                throw ex;<br />
                //MessageBox.Show(ex.Message, "Connection failure", MessageBoxButtons.OK, MessageBoxIcon.Error);<br />
            }<br />
        }<br />
        private void ThreadProcReceive()<br />
        {<br />
            for (; ; )<br />
            {<br />
                received = new byte[1024];<br />
                int readBytes = 0;<br />
                try<br />
                {<br />
                    readBytes = tcp.GetStream().Read(received, 0, received.Length);<br />
                }<br />
                catch (Exception)<br />
                {<br />
                    return;<br />
                }<br />
                if (readBytes == 8)<br />
                {<br />
                    StringBuilder shutMessage = new StringBuilder(8);<br />
                    for (int count = 0; count < 8; count++)<br />
                    {<br />
                        char ch = (char)received[count];<br />
                        shutMessage = shutMessage.Append(ch);<br />
                    }<br />
                    string shut = "shutdown";<br />
                    string receivedMessage = shutMessage.ToString();<br />
                    if (receivedMessage.Equals(shut))<br />
                    {<br />
                        //MessageBox.Show(this,"Shutdown Request has arrived from the \nconnected party.\nYou cannot send message anymore.\nPlease close the window.","Shut Down Request",MessageBoxButtons.OK,MessageBoxIcon.Information);<br />
                        DataArrived(new object(), new DataArrivalEventArgs("shutdown"));<br />
                        return;<br />
                    }<br />
                }<br />
                StringBuilder str = new StringBuilder(1024);<br />
                for (int count = 0; count < readBytes; count++)<br />
                {<br />
                    char ch = (char)received[count];<br />
                    str = str.Append(ch);<br />
                    //str = str.Append(" ");<br />
                }<br />
                GC.Collect();<br />
                DataArrived(new object(), new DataArrivalEventArgs(str.ToString()));<br />
            }<br />
<br />
        }<br />
        public void sendData(string sendData)<br />
        {<br />
            if (sendData.Length != 0)<br />
            {<br />
                char[] charArray = sendData.ToCharArray();<br />
                dataToSend = new byte[sendData.Length];<br />
                for (int charCount = 0; charCount < sendData.Length; charCount++)<br />
                    dataToSend[charCount] = (byte)charArray[charCount];<br />
            }<br />
            else<br />
                dataToSend = new byte[] { (byte)''e'', (byte)''m'', (byte)''p'', (byte)''t'', (byte)''y'' };<br />
            try<br />
            {<br />
                tcp.GetStream().Write(dataToSend, 0, dataToSend.Length);<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                throw ex;<br />
                //Connect(myipAddress, myPort);<br />
            }<br />
            GC.Collect();<br />
        }<br />
        public void Disconnect()<br />
        {<br />
            dataToSend = new byte[] { (byte)''s'', (byte)''h'', (byte)''u'', (byte)''t'', (byte)''d'', (byte)''o'', (byte)''w'', (byte)''n'' };<br />
            tcp.GetStream().Write(dataToSend, 0, dataToSend.Length);<br />
            tcp.Close();<br />
        }<br />
    }<br />
}

*************************************************** *************************
其余的代码是:

****************************************************** **********************

************************************************************************
and the rest of the code is :

************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace a
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string ip = "192.168.0.12";

            int port = 1341;
            TBMultiFunctionLibrary.ClientSocket.ClientSocket cs = new TBMultiFunctionLibrary.ClientSocket.ClientSocket(ip, port);

            if (CRMDataSetUtil.DesignerUtil.IsRunTime())
            {
                // TODO: Delete this line of code to remove the default AutoFill for 'cRMDataSet.test'.
                this.testTableAdapter.Fill(this.cRMDataSet.test);
                nUMBERTextBox.Text = " ";
            }

        }

        private void testButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.testTableAdapter.test(this.cRMDataSet.test, nAMETextBox.Text, nUMBERTextBox.Text);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);

            }


        }

        private void newMenuItemMenuItem_Click(object sender, EventArgs e)
        {
            testBindingSource.AddNew();
            a.testEditViewDialog testEditViewDialog = a.testEditViewDialog.Instance(this.testBindingSource);
            testEditViewDialog.ShowDialog();

        }

        private void testButton_Click_1(object sender, EventArgs e)
        {
            try
            {

                this.testTableAdapter.test(this.cRMDataSet.test, nAMETextBox.Text, nUMBERTextBox.Text);
            }
            catch (System.Exception ex)
            {

                System.Windows.Forms.MessageBox.Show(ex.Message);
            }


        }

        public void testDataGrid_Click(object sender, EventArgs e)
        {
            a.testSummaryViewDialog testSummaryViewDialog = a.testSummaryViewDialog.Instance(this.testBindingSource);
            testSummaryViewDialog.ShowDialog();
        }


    }
}




****************************************************** **********************
它不会给我任何错误
它尝试连接,但最后却给我一个错误
pop bouble到
****************************************************** **********************




************************************************************************
it doesnt give me any errors
it tryies to connect but at the end it gives me an error
pop bouble to
************************************************************************

catch (exception ex)
{
//connect (ipAddress, port);
throw ex;
//messageBox.show(ex.message, "connection failure", MessageBoxButtons.ok, MessageBoxIcon.error);
}


****************************************************** **********************
流行乐说:

无法建立连接,因为目标机器活动拒绝了它


据我了解,服务器不会重新放置,所以我真的不知道sql服务器是否有问题,例如是否需要许可或某些东西,或者代码中是否有其他东西
我认为这是在代码coz中,应用程序使用电缆(或cradel)工作gr8,问题是尝试通过ip
连接(检索或更改)数据库中的问题时
如果有人可以帮助我,那将是gr8,我真的迷路了

预先感谢您


************************************************************************
the pop bouble says :

no connection could be made because the target machine activity refuses it


which by what i understand is that the server wont respose so i realy dont know if there is a problem with the sql server like needs a permition or some thing or is it some thing in the code
i think it is in the code coz the application works gr8 using the cable (or cradel) the problem is when trying to connect (retrive or change ) in the database through ip

if any one can help me that would be gr8 i am realy lost

thank u in advance

推荐答案

,如果出现类型为目标计算机主动拒绝..."的错误消息,通常意味着所讨论的SQLserver可能未配置为从远程通过TCP/IP接收连接,或者目标计算机上的防火墙未配置为允许在默认端口1433上连接到SQLServer.

如果您可以直接访问SQLServer,请首先检查配置,以确保启用了来自远程的TCP/IP连接.

如果以上步骤无效,请检查防火墙设置.

希望对您有所帮助.
With an error message of the type "the target machine actively refused..." typically means that the SQLserver in question is possibly not configured to receive connections over TCP/IP from remote or that the firewall on the target machine is not configured to allow connections to the SQLServer on the default port of 1433.

If you have direct access to the SQLServer, first check the configuration to insure that TCP/IP connections from remote are enable.

Then check the firewall settings if the above step doesn''t work.

I hope this was helpful.


这篇关于使用sql server 2005的winodws移动C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆