Million times sorry for my question. I know there were a thousand questions exactly the same as mine. Still I couldn't find any solutions to my problem by reading earlier posts.
When I run my code, it doesn't throw any exceptions, it runs just fine, still doesn't add any data to my database when using the INSERT INTO
command nor alter the data when using UPDATE
command.
On the other hand, if I replace the code in the try section with a SELECT-FROM-WHERE
query, and read data with SqlDataReader
, it works just perfectly. So I assume there's no problem with the connection itself.
Here's my code:
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;
namespace adatbázis_gyakorlás_s_sharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string kapcslink;
kapcslink = adatbázis_gyakorlás_s_sharp.Properties.Settings.Default.kapcsolo;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(kapcslink);
con.Open();
try
{
string nev;
int irsz;
string telepules;
nev = "Joseph";
string lekerdezes = "INSERT INTO proba(nev) VALUES (@nev);";
System.Data.SqlClient.SqlCommand parancs = new System.Data.SqlClient.SqlCommand();
parancs.CommandText = lekerdezes;
parancs.CommandType = CommandType.Text;
parancs.Connection = con;
parancs.Parameters.Add("@nev", SqlDbType.VarChar).Value="Joseph";
parancs.ExecuteNonQuery();
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
con.Close();
}
}
}
Any suggestions?
Thanks in advance.