본문으로 건너뛰기

"c#" 태그로 연결된 4개 게시물개의 게시물이 있습니다.

모든 태그 보기

.NET에서 MySQL 사용자 변수 사용

· 약 1분

@value, @reg 같은 사용자 변수가 들어간 쿼리에서 오류가 발생한다.

Connection을 맺을 때 Allow User Variables=True 옵션을 추가해줘야한다.

string conf = "Server=$Server;Port=$port;Database=$DataBase;Uid=$User;Pwd=$Password;Allow User Variables=True";

C# String Split

· 약 1분

javascript 의 split 을 생각하고 split 을 사용했다간 예상치 못한 오류를 맞게된다.

일반 Split 함수는 Char 로만(한 글자로만) 자를 수 있다.

해결

string str = "cat__dog__animal__person";
string[] animals = Regex.Split(str, "__");

Regex.Split 을 사용하면 된다.

C# MySQL Singleton DB Connection

· 약 1분
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;

namespace WebApplication.ClassFolder{

public class DBConfig{
private static string Server = "";
private static string Database = "";
private static string ID = "";
private static string PASS = "";

static string conf = "Server=" + Server + ";Database=" + Database + ";Uid=" + ID + ";Pwd=" + PASS;

static MySqlConnection db;

public static MySqlConnection Connection{
get{
if (db == null){
LazyInitializer.EnsureInitialized(ref db, CreateConnection);
}
return db;
}
}

static MySqlConnection CreateConnection(){
var db = new MySqlConnection(conf);

db.Open();
return db;
}

// 강제로 커넥션을 끊어야될 경우가 있을 때
static void CloseConnection(){
if(db != null) {
db.Close();
db.Dispose();
db = null;
}
}

}
}

예제

// get Connection
var db = DBConfig.Connection;

.NET targetFramework 오류

· 약 1분

targetFramework가 인식할 수 없다고 페이지가 안 띄워지는 경우가 있다.

IIS의 .NET버전과 프로젝트의 .NET버전이 달라 발생한다. image from hexo

해결

IIS > 응용프로그램 풀 > 내 프로젝트 클릭 > 기본설정 메뉴에서 .NET Framework 버전 속성을 변경해준다. image from hexo