2
Answers

Issue with sqlexpress connection in windows forms application

Collins Garlinn

Collins Garlinn

15y
3.7k
1
Hello

I'm trying to develop a text-based rpg game in C# using sql express in C# 2008 express.  I'm still trying to figure out if the way that I want to develop the application is possilbe/reasonable.
  • use a richtext box and buttons for dialog and controls.
  • manage the sqlexpress connection using code not controls.
  • use a class structure to make the SqlConnection methods/members available to all controls on a form, as opposed to defining a new instance of the connection for every control.
Googling has given a number of console-oriented solutions, but nothing for windows forms that has matched. The closest I found demonstrated all of the connection activity being repeated per control.

what I've managed to get put together so far is very little.

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

namespace Trpg
{

public partial class TRPG : Form
{
//inserting class for sql connect here
public class Connect
{
SqlConnection tconnect;

public object TRPGConnect()
{
tconnect = new SqlConnection(@"server = .\sqlexpress;integrated security = true ;database = trpg");
return tconnect;
}
}
//end of sql connect class here


// create an instance of the connection.
Connect trpgc = new Connect();



public TRPG()
{
InitializeComponent();
}

private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void TRPG_Load(object sender, EventArgs e)
{


}

public void testConnect()
{
tconnect.Open();
tconnect.Close();
}
}
}


The error message I get is "error CS0103: The name 'tconnect' does not exist in the current context". I take it that is a scope issue, but I don't understand where my error is.

Any help is very much appreciated. I will keep looking to see if I can figure it out.



Answers (2)