cancel
Showing results for 
Search instead for 
Did you mean: 

connection can not be opened

Former Member
2,653

I am trying to connect sql anywhere12 database to my .net project but while connecting to the database but the connection can not be opened

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iAnywhere.Data.SQLAnywhere;

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

        private void btncon_Click(object sender, EventArgs e)
        {
         string connetionString = null;
         SAConnection connection = new SAConnection();
         SACommand command;
            //string sqla = null;
         connetionString = " ENG=demo12;UID=dba;PWD=sql;DBN=sql;LINKS=TCPIP";

         //  sqla = "INSERT INTO  Student(Rollnumber,Name) values('" + textBox1.Text + "','" + textBox2.Text + "')";
           // sqla = "SELECT * FROM Contacts";
            connection = new SAConnection(connetionString);
            try
            {
                connection.Open();
                command = new SACommand("SELECT * FROM Contacts", connection);
                command.ExecuteNonQuery();
                command.Dispose();
                connection.Close();
                MessageBox.Show(" ExecuteNonQuery in SqlCommand executed !!");
            }
            catch (Exception )
            {
                MessageBox.Show("Can not open connection ! ");
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

please help me why this things are happening inside the program im just a beginner in sql anywhere

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member

Your connection string will only connect to a running server ... and it is likely you have additional errors if you have such a server already running.

connetionString = " ENG=demo12;UID=dba;PWD=sql;DBN=sql;LINKS=TCPIP";

will only connect to a server with a "EngineName/ServerName" of 'demo12' and a "DataBaseName" of 'sql'. The latter may also need to be changed to DBN=demo if you have a server running with the sample demo.db running on it.

If you, otherwise, intend to use the SQL Anywhere created sample "Demo" data source you probably should change that to use DSN instead

connetionString = "DSN=SQL Anywhere 12 demo";

If you want to a server to autostart a database that you will need to at least add the DBF= parameter.